Question

I have the following requirements. I need to upload an Excel file to a MVC based site. For this I am using Kendo Upload. In the controller action that handles the upload I need to make a slight modification to the Excel file and then send it back as a file stream. I am using Aspose for the Excel modifications. My question is can I achieve all of this within the one controller action without the Excel file ever hitting the disk of web server?

Was it helpful?

Solution

I managed to get this to work by using the synchronous upload mode. My controller action looks like this:

[POST("SaveExcelFile")]
public FileStreamResult Save(IEnumerable<HttpPostedFileBase> files)
{
    // The Name of the Upload component is "files"
    if (files != null)
    {
        foreach (var file in files)
        {
            // Some browsers send file names with full path.
            // We are only interested in the file name.
            var fileName = Path.GetFileName(file.FileName);
            //var physicalPath = Path.Combine(Server.MapPath("~/App_Data"), fileName);

            Workbook excel2 = new Workbook(file.InputStream);
            excel2.Worksheets.Add("TEST");

            Stream stream = new MemoryStream();
            excel2.Save(stream, SaveFormat.Excel97To2003);

            stream.Position = 0;


            return File(stream, "application/vnd.ms-excel", "junk.xls");
            // The files are not actually saved in this demo
            // file.SaveAs(physicalPath);
        }
    }

    // Return an empty string to signify success
    return null;

}

This is only proof of concept code but you can get the idea of what I was trying to achieve. Upload a file, manipulate it and send the modified Worksheet back down to the client as a stream.

OTHER TIPS

I don't think you can. I have used KendoUI's upload control, and it seems that you'll only get to manipulate the file after it's written on the server side.

What you can do is to first save the file, perform your modification, then overwrite it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top