Yüklemek için, böyle bir şey kullanırsınız.
<form action="/MyController/SaveDocuments/" method="post" enctype="multipart/form-data">
<label for="file1">Document 1</label>
<input type="file" id="file1" name="file1" />
</form>
Ve burada dosyayı kaydetmek için denetleyici üzerinde kod:
... sen dizini "~/İçerik/Dosyalar" söylüyorlar, indirme gelince
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();
}
Sen onları rotanızda bırakmak zorundasınız.
routes.IgnoreRoute("Content/{*pathInfo}");