简体   繁体   中英

Upload / Download From Virtual Directories in ASP.NET C#

i want to be able to upload / Download Files from a specific folder in my ASP.NET WebApp, now since the app resides in the C:/ directory i didn't want to occupy the space in that partition, so i added a virtual directory pointing to the folder E:/Docs/Emps/ now i can upload fine .. but i can't find the the files in E:/Docs/Emps/ it's not there , and when i search for them the results come with a URL inside the virtual folder i created in IIS , now when i started to implement the download part .. i couldn't download it at all , i can't find / access[if i managed to find them] the files after too much time on Google, i tried to use WebClient but i'm getting this exception

Message
-----------
An exception occurred during a WebClient request.
-----------
Inner Exception
-----------
System.NotSupportedException: The given path's format is not supported. at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at System.Net.WebClient.DownloadFile(Uri address, String fileName)
-----------
Stack Trace
-----------
at System.Net.WebClient.DownloadFile(Uri address, String fileName) at System.Net.WebClient.DownloadFile(String address, String fileName) at APC_ERP.BusinessCore_EmployeesDocumentsCenter.imgbtnDownloadDocument_Click(Object sender, ImageClickEventArgs e)

here's the download code i'm using

WebClient Client = new WebClient();
string Path = Server.MapPath("~/"+(sender as ImageButton).CommandArgument);
string[]File=Path.Split('/');
string Destination = @"C:\" + File[File.Length - 1];
Client.DownloadFile(Path, Destination);
Client.Dispose();

The content of your variable Path seems incorrect. First of all a forward slash is not supported, secondly the tilde (~) might be an issue - I'm not sure about the latter one though. As we can't see your data you should debug and check this path variable as well as Destination .

Also, instead of using + it's recommended to use the static Combine method for path-related operations. Example:

Path.Combine(@"\\root\", (sender as ImageButton).CommandArgument);

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