要上传,您将使用这样的东西。
<form action="/MyController/SaveDocuments/" method="post" enctype="multipart/form-data">
<label for="file1">Document 1</label>
<input type="file" id="file1" name="file1" />
</form>
下面是控制器上保存文件的代码:
public Document[] SaveDocuments(HttpRequestBase iHttpRequest, Instruction instruction)
{
List<Document> documents = new List<Document>();
foreach (string inputTagName in iHttpRequest.Files)
{
HttpPostedFile file = iHttpRequest.Files[inputTagName];
if (file.ContentLength > 0)
{
if (Path.GetExtension(file.FileName).Length == 0)
{
throw new ValidationException(string.Format("File '{0}' has no extension (e.g. .doc .pdf)", file.FileName));
}
string filePath = documentService.BuildDocumentPath(instruction.InstructionId, file.FileName);
file.SaveAs(filePath);
documents.Add(new Document
{
Filename = Path.GetFileName(file.FileName),
Path = filePath
});
}
}
return documents.ToArray();
}
至于下载,假设你有目录“~/Content/Files”。。。
你只需要在你的路线上排除他们。
routes.IgnoreRoute("Content/{*pathInfo}");