2011-06-01 107 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)) 

乾杯, 克里斯