我正在使用以下代码上传文件
[HttpPost]
public ActionResult ImportDeleteCourse(ImportFromExcel model)
{
var excelFile = model.ExcelFile;
if (ModelState.IsValid)
{
OrganisationServices services = new OrganisationServices();
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
Path.GetFileName(excelFile.FileName));
excelFile.SaveAs(filePath);
// ... snipped //
}
}
我真的不需要存储上传的 Excel 文件。无论如何,我可以在不保存的情况下处理它吗?
注意:ImportFromExcel 类只不过是一个模型,它基本上是:
public class ImportFromExcel
{
[Required(ErrorMessage = "Please select an Excel file to upload.")]
[DisplayName("Excel File")]
public HttpPostedFileWrapper ExcelFile { get; set; }
}
最有趣的部分是它包装了一个 HttpPostedFileWrapper。
最佳答案
当然可以。正如 Patko 所建议的,InputStream 属性可用于另一个流。例如,我对上传的 xml 文档执行了此操作,以便与 LINQ to XML 一起使用:
XDocument XmlDoc = XDocument.Load(new StreamReader(viewmodel.FileUpload.InputStream))
干杯, 克里斯
关于asp.net-mvc - 处理上传的文件而不保存它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6199413/