开发者

Best way to transfer files through a web service

开发者 https://www.devze.com 2023-02-08 00:43 出处:网络
My C# program communicates with a server using a web service, I need the client to download big files from the server and have the option to pause and continue their download, the downloader must als

My C# program communicates with a server using a web service, I need the client to download big files from the server and have the option to pause and continue their download, the downloader must also be authorized to download the file.

I had two thoughts on how to do that,

one is to use some 3rd party API like wget to download the files. the problem with that is that I nee开发者_运维知识库d to learn the API commands and that I'm not certain I can show my download progress in the program, another issue is that I would have to use use bare URLs to get the files from the server which seems ugly and could lead to people just downloading them off the server (I want them to be authorized, although this isn't a real issue since this is just a school project).

My other thought was to create a method on the web service that will get a position in the file and an amount of bytes and return them and the client will piece them together, it seems more complicated but more compelling since the user must be authorized to download the file and I can use it to show the tester some more advanced programming skills ;). The issue with that looks like it might be performance taxing.

What's your opinion? what's the best way to download big files off a server?


Absent the need for authorization and partial downloads, WebClient.DownloadData or WebClient.DownloadDataAsync would be the preferred method of downloading a file from a server.

You could still use WebClient for the authorization by setting the Credentials in your WebClient object instance. If the user isn't authorized to download the file, based on those credentials, the server can return a 404 (Not found) or 403 (Forbidden).

If your server supports HTTP 1.1, the client can start in the middle of the file. To do so, you'll have to create a class that inherits from WebClient and override the GetWebRequest method. That method would then set the headers to do a positional GET.

class MyWebClient : WebClient
{
    public int StartDownloadAt { get; set; }
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(address);

        req.AddRange(position_to_start);
    }
}

And in the code that uses it:

MyWebClient client = new MyWebClient();
client.StartDownloadAt = 1024 * 2024; // start download 1 megabyte into file.
client.DownloadData(...);

The above is just an example. You'd probably want to make that more robust by having the StartDownLoadAt property reset to 0 when a download is done (or aborted), and not do the AddRange if StartdownloadAt is set to 0. To fully support ranges, you'd probably want properties for start and end range, etc.

And, of course, the client will have to handle stitching the disparate downloaded pieces together after download is complete.

The point is that it should be possible, with a little work, by using the WebClient class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号