Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The given path's format is not supported in IE

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?

like image 394
user3432681 Avatar asked Dec 04 '25 14:12

user3432681


1 Answers

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.

like image 175
Brad C Avatar answered Dec 07 '25 11:12

Brad C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!