Question

I have a C# Windows Phone 7.1 app that downloads a PDF file from a foreign web server and then (tries) to save it to the isolated storage area as a file. I have tried several different ways to get this done, but the file always ends up about 30% too large and when I open it up in a text editor, instead of seeing the USUAL 'PDF' characters at the start of the file followed by the encoded characters, I see basically junk. The test file I'm using is supposed to be 161k but when I view the file with the Isolated Storage Explorer, it's 271k.

First I download the file to a string. I inspected the string at this point in the debugger and it does contain the proper values and it is the correct length. The trouble happens when I try to write it to the isolated storage area. I tried both StreamWriter & BinaryWriter with identical invalid results. The contents of the resulting file appears to be a long stream of junk characters. Note, I am deleting the file if it exists just in case, before writing out the contents. Below is my code using the BinaryWriter version. What is wrong?

async public static Task URLToFileAsync(
                                string strUrl, 
                                string strDestFilename, 
                                IProgress<int> progress, 
                                CancellationToken cancelToken)
{
    strUrl = strUrl.Trim();

    if (String.IsNullOrWhiteSpace(strUrl))
        throw new ArgumentException("(Misc::URLToFileAsync) The URL is empty.");

    strDestFilename = strDestFilename.Trim();

    if (String.IsNullOrWhiteSpace(strDestFilename))
        throw new ArgumentException("(Misc::URLToFileAsync) The destination file name is empty.");

    // Create the isolated storage file.
    // FileStream fs = Misc.CreateIsolatedStorageFileStream(strDestFilename);
    IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();

    // Delete the file first.
    if (isoStorage.FileExists(strDestFilename))
        isoStorage.DeleteFile(strDestFilename);

    IsolatedStorageFileStream theIsoStream = isoStorage.OpenFile(strDestFilename, FileMode.Create);

    FileStream fs = theIsoStream;

    // If the stream writer is NULL, then the file could not be created.
    if (fs == null)
        throw new System.IO.IOException("(Misc::URLToFileAsync) Error creating or writing to the file named: " + strDestFilename);

    BinaryWriter bw = new BinaryWriter(fs);

    try
    {

        // Call URLToStringAsync() to get the web file as a string first.
        string strFileContents = await URLToStringAsync(strUrl, progress, cancelToken);

        // >>>> NOTE: strFileContents looks correct and is the correct size.

        // Operation cancelled?
        if (!safeCancellationCheck(cancelToken))
        {
            // Note.  BinaryWriter does not have an Async method so we take the hit here
            //  to do a synchronous operation.
            //  See this Stack Overflow post.
            // http://stackoverflow.com/questions/10315316/asynchronous-binaryreader-and-binarywriter-in-net

            // >>>> NOTE: strFileContents.ToCharArray() looks correct and is the correct length.
            bw.Write(strFileContents.ToCharArray(), 0, strFileContents.Length);
        } // if (safeCancellationCheck(cancelToken))
    }
    finally
    {
        // Make sure the file is cleaned up.
        bw.Flush();
        bw.Close();

        // Make sure the file is disposed.
        bw.Dispose();
    } // try/finally

    // >>>> NOTE: output file in Isolated Storage Explorer is the wrong size and contains apparently junk.
} // async public static void URLToFileAsync
Was it helpful?

Solution

You cannot download a binary into a string. The result will not be correct, as you have found out.

See this answer, which demonstrates how to download a binary file to isolated storage: https://stackoverflow.com/a/6909201/1822514

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top