简体   繁体   English

如何将下载的文件保存到本地文件夹?

[英]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.

相关问题 如何将下载的 PDF 文件直接保存到文件夹中? - How do I save my downloaded PDF file directly into a folder? 如何将从url(服务器)下载的图像保存到Windows应用商店中的本地文件夹 - how to save image downloaded from url(server) to local folder in windows store app 如何将 aspx 文件另存为 html 文件和文件夹到本地计算机或服务器 - how to save aspx file as html file and folder to local machine or server 如何将已下载的Excel文件数据从数据表保存到ASP.NET中的应用程序文件夹中 - How to save downloaded Excel file data from Data table into application folder in ASP.NET 如何将Report Pdf文件保存到我的本地文件夹 - How to save Report Pdf file to my local folder 如何将应用程序的本机文件保存到本地文件夹? - How to save an application's native file to a local folder? 如何将文件从服务器文件夹保存到本地驱动器/客户端? - How save file from server folder to local drive/client side? 如何上传文件并将其保存在本地计算机的特定文件夹中? - How to upload a file and save it in an specific folder in local computer? 将 pdf 文件保存在本地文件夹中或下载 - save an pdf file in local folder or downloads c#如何将IO.Stream保存到MemoryStream并保存到本地文件夹上的PDF文件 - c# how to save IO.Stream to MemoryStream and save to PDF file on local folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM