I want to save a uploaded excel file in asp.net. In Chrome this works good:
if (Request.Files.Count > 0)
{
var currentUser = User.Identity.GetUserId();
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = string.Format(DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss_") + file.FileName);
var path = Path.Combine(Server.MapPath("~/Documents/"), string.Format("{0}", fileName));
file.SaveAs(path);
uploadModel.Document = fileName;
}
}
In Chrome is the var fileName "2015-06-29 14_33_53_example.xlsx" and in IE the fileName is "2015-06-29 14_21_00_C:\example.xlsx". And throw the error by executing file.SaveAs(path);
How can I support IE?
Try using System.IO.Path.GetFileName to parse out the full path that IE sends. This should work on all browsers:
var parsedFileName = Path.GetFileName(file.FileName);
var fileName = string.Format(DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss_") + parsedFileName );
Here is a link to a MSDN blog detailing the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With