Reputation: 10712
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:
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>
Upvotes: 0
Views: 352
Reputation: 6916
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);
}
}
}
Upvotes: 1