简体   繁体   中英

The given path's format is not supported

I am trying to downlaod an excel sheet and I am dynamically generating its filename which has to be in the following format:

eg:

User_Wise_List_Of_Documents_2013_On_16_04_2013

For this I wrote the following code:

string currentDate = DateTime.Now.Date.ToString();

string currentYear=DateTime.Now.Year.ToString();

filename = Server.MapPath("~/User/Documents/") + "User_Wise_List_Of_Documents_" + currentYear + " on " + currentDate + ".xls";

Somehow, its giving me the following exception:

The given path's format is not supported.

Any help will be appreciated.

I think your filename contains invalid chars like : Because you are forming filename with string currentDate = DateTime.Now.Date.ToString() .

See the list for invalid chars

var invalidChars = Path.GetInvalidFileNameChars();

EDIT

You can use this to replace invalid chars

string newdatestr = String.Join("",currentDate.Select(c => invalidChars.Contains(c) ? '_' : c));

Have you checked the result of Server.MapPath("~/User/Documents/") ?

It will return the directory path of your website's folder "/User/Documents/" , which would be for example "C:\\wwwroot\\User\\Documents\\" . You cant download a file from there (unless you're hosting local, in which case it might work, but for you only.

Probably you're looking for the Page.ResolveUrl() function, which will create a web uri relative to your website, rather than to the filesystem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM