2011-06-01 105 views
2

我使用下面的代码处理上传的文件而不保存它?

[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。

+0

只是保存到的MemoryStream? – jakubmal 2011-06-01 10:05:11

+0

我是.NET新手 - 我不知道该怎么做。 – Extrakun 2011-06-01 10:17:11

+0

这是什么ImportFromExcel类?你能展示它的方法,领域,无论给出任何线索吗?尝试使用反射器http://reflector.red-gate.com/download.aspx或在Visual Studio中列出它们 – jakubmal 2011-06-01 10:20:20

回答

3

当然可以。正如Patko建议的那样,InputStream属性可以用于另一个流。例如,我这样做是对上载的XML文档与LINQ使用到XML:

XDocument XmlDoc = XDocument.Load(new StreamReader(viewmodel.FileUpload.InputStream)) 

干杯, 克里斯