외부 웹 서버에서 PDF 파일을 다운로드 한 다음 격리 된 저장 영역에 PDF 파일을 다운로드하는 C# Windows Phone 7.1 응용 프로그램이 있습니다. 파일로. 이 작업을 수행하는 데 여러 가지 방법을 시도했지만 파일 크기가 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
@chue_x :
는 격리 된 저장소에 바이너리 파일을 다운로드하는 방법을 보여줍니다이 답변을 참조하십시오. 감사. 그 일을했지만 왜, 특히 문자열의 길이가 웹 파일의 크기와 동일하기 때문에 이유를 알고 싶습니다. 차이점은 내가 생각할 수있는 유일한 이유는 C# /. NET은 멀티 바이트 인코딩 또는 문자열 메모리 자체의 다른 인코딩과 함께 문자열을 저장한다는 것입니다. 적어도 비 -.NET C, C++ 및 Delphi는 그렇게하지 않습니다. . –
@RobertOschler ** .NET의 모든 문자열은 UTF-16 **으로 인코딩됩니다. 따라서 문자열을 읽을 때 런타임은 문자를 해당 인코딩으로 변환하려고 시도합니다. 소스가 사용한 인코딩을 찾을 수없는 경우 기본값은 UTF-8입니다. 따라서 기본적으로 .NET은 PDF 파일이 UTF-8로 인코딩 된 텍스트라고 생각하고이를 UTF-16으로 변환합니다. 두 인코딩 모두 동일한 이진 표현이 없으므로 데이터가 손상된 이유를 설명합니다. –
@KooKiz - 감사합니다. 모든 것을 바이트 배열로 바꾸 었으며 이제는 모두 잘되었습니다. –