[英]How to save downloaded file to local folder?
I want to store file which is coming from API, i have tried the below code but it is giving error. 我想存储来自API的文件,我已经尝试了以下代码,但给出了错误。
string StrFileName = result.LabelDownload.Href;
string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
string dir = string.Format("{0}{1:yyyy-MM}\\", FilePath, DateTime.Now);
// Save the uploaded file.
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
WebClient wc = new WebClient();
wc.DownloadFile(StrFileName, dir);
Error: An exception occurred during a WebClient request. 错误:WebClient请求期间发生异常。 Inner Exception: The filename, directory name, or volume label syntax is incorrect.
内部异常:文件名,目录名称或卷标签语法不正确。
how i can download file from web and store it my local folder? 如何从Web下载文件并将其存储在本地文件夹中? i need to save that path in database
我需要将该路径保存在数据库中
The WebClient.DownloadFile
method requires the filename where to download the file, not just the directory. WebClient.DownloadFile
方法需要下载文件的文件名,而不仅仅是目录。 So either pass just the filename and it will save the file to the apps directory or provide the fully qualified path. 因此,要么只传递文件名,它将文件保存到apps目录,要么提供完全限定的路径。
string sourceFileName =
result.LabelDownload.Href;
string filePath = ConfigurationManager.AppSettings["FilePath"].ToString();
string dir = string.Format("{0}{1:yyyy-MM}", filePath, DateTime.Now);
// Save the uploaded file.
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
WebClient wc = new WebClient();
wc.DownloadFile(sourceFileName, Path.Combine(dir, sourceFileName));
The same goes for the source file. 源文件也是如此。 Here is the doc .
这是文档 。
Also take a look at c# naming conventions as local variables are usually lower case. 还要看看c#命名约定,因为局部变量通常是小写的。
I solve it, 我解决了
WebClient wc = new WebClient();
wc.DownloadFile(StrFileName, dir + filenameWithoutPath);
before i wasn't added filename, so we need to add filename, in my case it is filenameWithoutPath, it is working now. 在没有添加文件名之前,所以我们需要添加文件名,在我的情况下是filenameWithoutPath,它现在可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.