Hobbit7
Hobbit7

Reputation: 333

What exactly is HttpResponse.Content.Headers.ContentDisposition?

Can I store values like the version number of a file in HttpResponse.Content.Headers.ContentDisposition?

I need to write the version number of a file in the headers or somewhere else because I want to find out if the client has stored the newest version of a certain file on his iOS/Android smart phone.

For example: Today, the client downloads the very first time a file named MyFile.txt from a server. Tomorrow, the client needs to check if there is a new version of MyFile.txt on the server. If yes, download it. If no, don't download it.

Where can I store the version number of a text file so that the client can find out if the file on the server is newer than the stored version?

Is it possible to store it in HttpResponse.Content.Headers.ContentDisposition or is it necessary that I always create a new file and file name, for example MyFileVersion1.txt, MyFileVersion2.txt, ...?

This is how I download a file. I use new ContentDisposition(httpResponse.Content.Headers.ContentDisposition.ToString()).FileName to get the file name.

string DownloadFile = await DownloadFileAsync(URL);

async Task<string> DownloadFileAsync(string fileUrl)
{
var _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(15) };

try
{
    using (var httpResponse = await _httpClient.GetAsync(fileUrl))
    {
        if (httpResponse.StatusCode == HttpStatusCode.OK)
        {
            string localfilename = new ContentDisposition(httpResponse.Content.Headers.ContentDisposition.ToString()).FileName;
            byte[] byteresponse = await httpResponse.Content.ReadAsByteArrayAsync();
            string documentsPathnew = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            string localPathnew = Path.Combine(documentsPathnew, localfilename);
            File.WriteAllBytes(localPathnew, byteresponse);
            return "Saved file: " + localfilename;
        }
        else
        {
            return "Url is invalid";
        }
    }
}
catch (Exception)
{
    return "Exception. Something went wrong.";
}
}

Upvotes: 0

Views: 164

Answers (1)

GarlicFries
GarlicFries

Reputation: 8323

RFC 2616 states that

The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file.

Why not just use a NameValueHeaderValue with a name of Version and a value of whatever you want?

Upvotes: 1

Related Questions