2013-04-09 68 views
0

我有一个C#Windows Phone 7.1应用程序,它从外部Web服务器下载PDF文件,然后(尝试)将其保存到独立存储区作为一个文件。我已经尝试了几种不同的方法来完成这个工作,但是文件总是会大大超过30%,而当我在文本编辑器中打开文件时,文件开始时会看到USUAL'PDF'字符,然后是编码的字符,我基本上看到垃圾。我使用的测试文件应该是161k,但是当我用Isolated Storage Explorer查看文件时,它是271k。将字节数组写入C#中的独立存储区文件Windows Phone 7应用程序无效

首先我将文件下载到一个字符串。我在调试器的这一点检查了字符串,它确实包含了正确的值,并且它是正确的长度。当我尝试将它写入隔离的存储区域时发生问题。我试过StreamWriter & BinaryWriter具有相同的无效结果。生成的文件的内容似乎是一长串垃圾字符。请注意,如果文件存在以防万一,在写出内容之前,我正在删除该文件。以下是我使用BinaryWriter版本的代码。哪里不对?

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 

回答

1

您无法将二进制文件下载到字符串中。结果将不正确,正如你发现的那样。

看到这个答案,它演示了如何下载二进制文件到独立存储:https://stackoverflow.com/a/6909201/1822514

+0

@chue_x。谢谢。这工作,但我想知道为什么,特别是因为字符串的长度是相同的网页文件的大小。我能想到的唯一原因是C#/ .NET在字符串内存中存储了多字节编码或其他编码的字符串,至少非.NET C,C++和Delphi没有这样做。 – 2013-04-09 03:47:16

+2

@RobertOschler ** .NET中的所有字符串都以UTF-16 **编码。所以在读取字符串时,运行时会尝试将字符转换为该编码。当无法找出源使用的编码时,它将默认为UTF-8。所以基本上,在你的情况下,.NET认为你的PDF文件是一个UTF-8编码文本,并将其转换为UTF-16。由于两种编码都不具有相同的二进制表示形式,因此它解释了数据被破坏的原因。 – 2013-04-09 05:07:25

+0

@KooKiz - 解释它,谢谢。我已将所有内容切换到字节数组,现在一切正常。 – 2013-04-09 05:15:40

相关问题