In this article we'll talk about downloading the full and partial contents of a file from a server. Then we'll see if you can do it via WebAPI. Before we jump to the main discussion we'll look at some basic things. So let's use the simple case of when you try to download a file from the server, what a header actually contains in a request and response. For example, you want to download a file, say data.zip. Before sending the request you can run fiddler to trace your request and response.
When you send the request the first time you'll see the Request head will contain information something like this:
GET /files/data.zip HTTP/1.1
Accept-Language: en/us,en
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Now if you check the response then It'll look something as in the following:
HTTP/1.1 200 OK
Content-Type: application/x-zip-compressed
Last-Modified: Mon, 18 June 2013 07:15:20 GMT
Accept-Ranges: bytes
ETag: "e2827cfd8e57ce1:0"
Server: Microsoft-IIS/8.0
Date: Mon, 17 Jun 2013 09:01:22 GMT
Content-Length: 62887922
So if you want to know that if the server is available to serve the partial content then look at the attribute:
Accept-Ranges: bytes
If this attribute is present with value bytes then that means the client can request the partial content to download. If this attribute is not present in the response or has a value none then the server won't accept the partial content request.
Partial content request headers
Let's have a look at the partial content headers; how they look like when a request is made for them.
GET /files/data.zip HTTP/1.1
Accept-Language: en/us,en
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
If-Match: "dfTGdsdIofQXrAY3ZddfSd=="
Range: bytes=22234-
Here if you observe, the Range header has the value specified in Bytes with –. It basically follows the format, for example Range: {from}-{To}. If nothing is specified in the To part then it'll read the file to the end and write it in the response. Otherwise if a value is provided then it'll read that portion only. Now let's look at the response of this request.
HTTP/1.1 206 OK
Content-Range: bytes 22234-62887922/62887923
Content-Length: 62887922
Content-Type: application/x-zip-compressed
Last-Modified: Mon, 18 June 2013 09:15:20 GMT
ETag: "dfTGdsdIofQXrAY3ZddfSd=="
Accept-Ranges: bytes
Binary content of data.zip from byte 500,001 onwards...
If you want to learn more about the range specific request in the HTTP protocol then you can have a look at the HTTP Spec.
Now, let's return to the business; why we are discussing all this stuff. I have a ASP.Net MVC WebAPI that is actually serving me the file based on the query parameters. Now in this case the scenario will change. We cannot send the partial request directly to the Controller. If we do need to write the custom code to handle the partial requests. If you're thinking to write the HttpHandlers at this moment then I should tell you that "Controller are new Handlers in WebAPI". Jeff Fritz wrote a nice article on this discussion.
So first you can start creating a simple ASP.Net WebAPI project. Let's say add a DownloadController in the contollers folder. We have a method, say DownloadFile in it. So when we send a request to download a file then it'll look something like this. The following sample is just reading the file from disk and returning the complete file.
- public class DownloadController : ApiController
- {
- public HttpResponse DownloadFile(string fileName)
- {
-
- HttpResponseMessage result = null;
- var fullFilePath = Path.Combine(this.packageDirectoryFilePath, fileName);
-
-
- FileStream sourceStream = File.Open(fullFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
- BufferedStream bs = new BufferedStream(sourceStream);
-
- result = new HttpResponseMessage(HttpStatusCode.OK);
- result.Content = new StreamContent(bs);
-
- result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeType);
- result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
- {
- FileName = fileName
- };
-
- return result;
- }
- }
So to send the partial content request you need to send a partial content request something. I've written a custom function to create and send a partial content request or so to say Range specific request.
Client_Program.cs
- private static void DownloadFileWithRangeSpecificRequests(string sourceUrl, string destinationPath)
- {
- long existLen = 0;
- System.IO.FileStream saveFileStream;
- if (System.IO.File.Exists(destinationPath))
- {
- System.IO.FileInfo fINfo = new System.IO.FileInfo(destinationPath);
- existLen = fINfo.Length;
- }
- if (existLen > 0)
- saveFileStream = new System.IO.FileStream(destinationPath,
- System.IO.FileMode.Append, System.IO.FileAccess.Write,
- System.IO.FileShare.ReadWrite);
- else
- saveFileStream = new System.IO.FileStream(destinationPath,
- System.IO.FileMode.Create, System.IO.FileAccess.Write,
- System.IO.FileShare.ReadWrite);
-
- System.Net.HttpWebRequest httpWebRequest;
- System.Net.HttpWebResponse httpWebResponse;
- httpWebRequest = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(sourceUrl);
- httpWebRequest.AddRange((int) existLen);
- System.IO.Stream smRespStream;
- httpWebResponse = (System.Net.HttpWebResponse) httpWebRequest.GetResponse();
- smRespStream = httpWebResponse.GetResponseStream();
- var abc = httpWebRequest.Timeout;
-
- smRespStream.CopyTo(saveFileStream);
- saveFileStream.Close();
- }
This will actually read the file from file system and if it's not fully downloaded then send the request from the last byte of the file. So if you try to run the application with the default downloader and the client then you'll always get the complete file. Since the DownloadController doesn't know about the request if it's a Range specific. So let's modify the controller and add some checks to receive the RangeSpecific requests and treat them specially.
Update DownloadController.cs
-
-
-
-
-
- public HttpResponseMessage DownloadFile(string fileName)
- {
- this.LogRequestHttpHeaders(this.logFilePath, Request);
-
- HttpResponseMessage result = null;
- var fullFilePath = Path.Combine(this.packageDirectoryFilePath, fileName);
-
- if (Request.Headers.Range == null || Request.Headers.Range.Ranges.Count == 0 ||
- Request.Headers.Range.Ranges.FirstOrDefault().From.Value == 0)
- {
-
- FileStream sourceStream = File.Open(fullFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
- BufferedStream bs = new BufferedStream(sourceStream);
-
- result = new HttpResponseMessage(HttpStatusCode.OK);
- result.Content = new StreamContent(bs);
-
- result.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeType);
- result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
- {
- FileName = fileName
- };
- }
- else
- {
-
- var item = Request.Headers.Range.Ranges.FirstOrDefault();
- if (item != null && item.From.HasValue)
- {
- result = this.GetPartialContent(fileName, item.From.Value);
- }
- }
-
- this.LogResponseHttpHeaders(this.logFilePath, result);
-
- return result;
- }
-
-
-
-
-
-
-
- private HttpResponseMessage GetPartialContent(string fileName, long partial)
- {
- var fullFilePath = Path.Combine(this.packageDirectoryFilePath, fileName);
- FileInfo fileInfo = new FileInfo(fullFilePath);
- long startByte = partial;
-
- Action<Stream, HttpContent, TransportContext> pushContentAction = (outputStream, content, context) =>
- {
- try
- {
- var buffer = new byte[65536];
- using (var file = File.Open(fullFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- var bytesRead = 1;
- file.Seek(startByte, SeekOrigin.Begin);
- int length = Convert.ToInt32((fileInfo.Length - 1) - startByte) + 1;
-
- while (length > 0 && bytesRead > 0)
- {
- bytesRead = file.Read(buffer, 0, Math.Min(length, buffer.Length));
- outputStream.Write(buffer, 0, bytesRead);
- length -= bytesRead;
- }
- }
- }
- catch (HttpException ex)
- {
- this.LogException(ex);
- }
- finally
- {
- outputStream.Close();
- }
- };
-
- HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.PartialContent);
- result.Content = new PushStreamContent(pushContentAction, new MediaTypeHeaderValue(MimeType));
- result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
- {
- FileName = fileName
- };
-
- return result;
- }
So now we have added some conditions to check if a request is range specific or it's a full content request. To server the partial content I've written a custom function GetPartialContent() that uses the file name and the startByte as the argument. Also we have used the PushStreamContent to write the content to the response.
Now your WebAPI is ready to serve the content with a Range specific request.
But to tell you the update this case has already been taken into the account of my Microsoft ASP.Net team and they've included the Byte specific request handlers in the NightlyBuilds that are available via Nugets. But I need to write it as updating the production with the latest Pre-release which could be an impact when the RTM is available.
Complete source code:
DownloadController.cs
Client.cs