-1

i'im downloading a PDF file to the Isolated Storage via WebClient class and OpenReadAsync method. Once i save the file to the IsolatedSotrage i name it "file.pdf" but i need to keep the original name. How can i do that? I did some research and i understand that get the file name before download is difficult and not very convenient because some header info may be missing. But after download? Can i get it in download.OpenReadCompleted completed method? simply did not know ho to do that.

Thanks all.

3
  • 1
    This question is lacking a lot of information - where are you downloading from, what is a sample URL (can we extract the filename from there), is this a service that you can control (so can send through the filename as a property) etc. etc.
    – Oren
    Jun 11, 2013 at 19:11
  • No I'm simply developing a downloader that store some file in the IsolatedStorage. I obviously can extract the name from the URL, ok, but not every URL is is simply showed as www.website.com/filename.pdf. I just wonder if is there any way to access file's name and type after download. Jun 11, 2013 at 21:40
  • Here's an example, Geek Champ's book download: geekchamp.com/marketplace/components/… Jun 11, 2013 at 22:02

2 Answers 2

0

One would assume you know the file name you are downloading by the URL, or at least you can parse it. Assuming that, you can pass it to the event handler via the UserState object:

myClient.OpenReadAsync(url, filenameFromUrl);

Then, in the event handler:

void OnOpenReadCompleted(OpenReadCompletedEventArgs e)
{
   string filename = e.UserState.ToString();
}

If you don't know the URL or can't get the filename because it's some sort of web service that masks it, then no, you can't get it from the event args.

1
  • Thanks a lot, but I need to preserve the original name also from different URLs which are does not simply contain the file name like "www.website.com/filename.pdf". Is there a way to organize code better? Jun 11, 2013 at 21:23
0

You need to use HttpWebRequest and grab the response headers. The below is dirty proof of concept code, but it can be easily integrated into whatever flow you already have:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        HttpWebRequest req = HttpWebRequest.CreateHttp("http://www.geekchamp.com/marketplace/components/windows-phone-toolkit-in-depth-3rd-edition/downloadfree?id=381255");

        req.BeginGetResponse(new AsyncCallback(ReadCallback), req);
    }

    private void ReadCallback(IAsyncResult asynchronousResult)
    {
        HttpWebRequest req = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(asynchronousResult);

        // RegEx to extract file name from headers
        var reFile = new Regex("filename=\"(.*?)\"");

        // The header that contains the filename. Example:
        // Content-Disposition: attachment; filename="Windows Phone Toolkit In Depth 3rd Abstract.pdf"
        var contentDisposition = response.Headers["Content-Disposition"];

        // FIXME: this assumes match success. Might be easier to just use a replace
        var filename = reFile.Match(contentDisposition).Groups[1].Value;

        // ... your code here ...
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.