如何从服务器下载文件并将其保存到本地计算机? [英] How to download a file from server and save it my local machine?

查看:791
本文介绍了如何从服务器下载文件并将其保存到本地计算机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从服务器中选择文件夹(静态),文件应该下载到我创建的文件夹中。



我试过的:



我尝试从服务器下载文件,但它完全是静态的我给出了服务器的位置以及我需要的位置保存。



我只需要提供服务器的URL,但文件应该自动存储在通过代码创建的文件夹中。



此代码完全静态且有效。



I need to select the folder from the server (static) and the file should be downloaded in into my created folder.

What I have tried:

I tried downloading the file from the server but it is completely static I am giving the location of the server as well as a location where I need to save.

I just need to give the URL of the server but the file should be automatically stored in the folder created through code.

This code is completely static and it's working.

WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
webClient.DownloadFileAsync(new Uri(@"\\192.168.75.99\Developer shared folder\Icons\chemistrywhite.png"), @"C:\photos\chemistrywhite.png");





我动态创建了一个文件夹。进入这个,我的服务器文件(chemistrywhite.png)应该下载



I created a folder dynamically. Into this, my server file(chemistrywhite.png) should be downloaded

string folderName = @"C:\";
string pathString = System.IO.Path.Combine(folderName, "storefolder");
Directory.CreateDirectory(pathString);
string fileName = System.IO.Path.GetRandomFileName();
pathString = System.IO.Path.Combine(pathString, fileName);

if (!File.Exists(pathString))
{
    using (FileStream fs = File.Create(pathString))
    {
        for (byte i = 0; i < 100; i++)
        {
            fs.WriteByte(i);
        }

    }
}
else
{
    Console.WriteLine("File \"{0}\" already exists.", fileName);
    return;
}

推荐答案

如果您的下载工作正常,您想要更改的是路径它已保存,您可以这样做:



If your download is working fine and all you want to change is the path to which it's saved, you can just do:

string folderName = @"C:\";
string pathString = System.IO.Path.Combine(folderName, "storefolder");
Directory.CreateDirectory(pathString);
string fileName = System.IO.Path.GetRandomFileName();
pathString = System.IO.Path.Combine(pathString, fileName);

if (!File.Exists(pathString))
{
    WebClient webClient = new WebClient();
    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
    webClient.DownloadFileAsync(new Uri(@"\\192.168.75.99\Developer shared folder\Icons\chemistrywhite.png"), pathString);
}
else
{
    Console.WriteLine("File \"{0}\" already exists.", fileName);
    return;
}


这篇关于如何从服务器下载文件并将其保存到本地计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆