0

I'm trying to create a razor function that will allow the user to create a file upload form.

the parameters of the function are:

  • ContentPath - the path on the server where the file will be stored
  • FilePrefix - optional prefix
  • FileSuffix - optional suffix

I wonder if something like this have already been done? And if not I can seem to understand where sholud the form post to ? where would the code that actuali makes the upload be ?

this is what i have so far.

@inherits RazorFunction

@functions {
    public override string FunctionDescription
    {
        get  { return "This is a file upload component."; }
    }

    [FunctionParameter(Label="Content Path", Help="Relative path of the folder that the files sholud be sotred in.", DefaultValue = "/Uploads")]
    public string ContentPath { get; set; }

    [FunctionParameter(Label = "File Prefix ", Help = "A prefix to append to the uploaded file.", DefaultValue = "")]
    public string FilePrefix { get; set; }

    [FunctionParameter(Label = "File Suffix ", Help = "A suffix to append to the uploaded file (befire the extention).", DefaultValue = "")]
    public string FileSuffix { get; set; }
}

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
    <head>
        @* script, css etc. you add in this head section end up in the head on the rendered page *@
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" action="/Controller?">
            <input type="file" name="files[]" id="files[]" multiple="multiple" />
            <input type="submit" value="Upload files"/>
        </form>
    </body>
</html>
Charles
  • 50,943
  • 13
  • 104
  • 142
Mortalus
  • 10,574
  • 11
  • 67
  • 117
  • I have no idea how `composite-c1` effects the scenario - but in a "normal" asp.net/mvc/razor solution, the code that receives your file (from the `form`) and persists it into your file system, would be in your controller (decorated with a `[HttpPost]`). The `action` on your form would point at this `controller/action`. I can post some (non `composite-c1`) code if you think it may point you in a useful direction. – dav1dsm1th Nov 15 '13 at 06:48
  • yes.. i know that ... but composite c1 takes charge of te routing engine so i don't really know where should i place the .cs that should be responsible for the actual upload. – Mortalus Nov 15 '13 at 11:33
  • You can just do a @if(IsPost) block in the razor and let it post back to itself and then handle it directly in the razor function. – Poul K. Sørensen Dec 19 '13 at 10:02

1 Answers1

1

Just leave the action-attribute of the form empty and add the following code in top of the Razor file

@{
    if (IsPost) 
    {
        foreach (HttpPostedFile file in Request.Files)
        {
            var fileName = FilePrefix + file.FileName;
            fileName = Path.GetFileNameWithoutExtension(fileName) + FileSuffix + Path.GetExtension(file.FileName);
            var path = Path.Combine(ContentPath, fileName);

            file.SaveAs(path);
        }
    }
}
Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48