Extrakun
Extrakun

Reputation: 19325

Processing an uploaded file without saving it?

I am uploading a file using the following code

[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 //
  }
}

I do not really need to do store the uploaded excel file. Is there anyway I can process it without saving?

Note: The ImportFromExcel class is nothing but a model, which is basically:

  public class ImportFromExcel
    {
        [Required(ErrorMessage = "Please select an Excel file to upload.")]
        [DisplayName("Excel File")]
        public HttpPostedFileWrapper ExcelFile { get; set; }
    }

The most interesting part is that it wraps a HttpPostedFileWrapper.

Upvotes: 2

Views: 2568

Answers (2)

Christopher
Christopher

Reputation: 517

Sure you can. As Patko suggested, the InputStream property can be used for another stream. For example I did this for an uploaded xml document to use with LINQ to XML:

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

Cheers, Chris

Upvotes: 3

Patko
Patko

Reputation: 4423

The HttpPostedFileBase.InputStream property looks promising. You should be able to use that and save the data to whichever other stream you need to.

Upvotes: 2

Related Questions