Trong ứng dụng của tôi, tôi có thể tải xuống một số tệp phương tiện từ web. Thông thường tôi đã sử dụng phương pháp WebClient.OpenReadCompleted để tải xuống, giải mã và lưu tệp vào IsolatedStorage. Nó hoạt động tốt và nhìn như thế:Tải xuống tệp theo khối (Windows Phone)
private void downloadedSong_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e, SomeOtherValues someOtherValues) // delegate, uses additional values
{
// Some preparations
try
{
if (e.Result != null)
{
using (isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
// working with the gained stream, decryption
// saving the decrypted file to isolatedStorage
isolatedStorageFileStream = new IsolatedStorageFileStream("SomeFileNameHere", FileMode.OpenOrCreate, isolatedStorageFile);
// and use it for MediaElement
mediaElement.SetSource(isolatedStorageFileStream);
mediaElement.Position = new TimeSpan(0);
mediaElement.MediaOpened += new RoutedEventHandler(mediaFile_MediaOpened);
// and some other work
}
}
}
catch(Exception ex)
{
// try/catch stuff
}
}
Nhưng sau một cuộc điều tra tôi phát hiện ra rằng với các tập tin lớn (đối với tôi nó là hơn 100 MB) Tôi nhận được OutOfMemory ngoại lệ trong tải tập tin này. Tôi cho rằng đó là vì WebClient.OpenReadCompleted tải toàn bộ luồng vào RAM và cuộn cảm ... Và tôi sẽ cần thêm bộ nhớ để giải mã luồng này. Sau khi điều tra khác, tôi đã tìm thấy cách chia tập tin lớn thành các khối sau khi OpenReadCompleted sự kiện khi lưu tệp này vào IsolatedStorage (hoặc giải mã và sau đó lưu trong ocasion của tôi), nhưng điều này sẽ giúp chỉ với một phần của vấn đề. .. Vấn đề chính là cách ngăn chặn điện thoại bị gián đoạn trong quá trình tải xuống. Có cách nào để tải xuống tệp lớn theo khối không? Sau đó, tôi có thể sử dụng giải pháp tìm thấy để vượt qua quá trình giải mã. (Và tôi vẫn sẽ cần phải tìm một cách để tải tập tin lớn như vậy vào MediaElement, nhưng đó sẽ là một câu hỏi khác)
Trả lời:
private WebHeaderCollection headers;
private int iterator = 0;
private int delta = 1048576;
private string savedFile = "testFile.mp3";
// some preparations
// Start downloading first piece
using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorageFile.FileExists(savedFile))
isolatedStorageFile.DeleteFile(savedFile);
}
headers = new WebHeaderCollection();
headers[HttpRequestHeader.Range] = "bytes=" + iterator.ToString() + '-' + (iterator + delta).ToString();
webClientReadCompleted = new WebClient();
webClientReadCompleted.Headers = headers;
webClientReadCompleted.OpenReadCompleted += downloadedSong_OpenReadCompleted;
webClientReadCompleted.OpenReadAsync(new Uri(song.Link));
// song.Link was given earlier
private void downloadedSong_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
try
{
if (e.Cancelled == false)
{
if (e.Result != null)
{
((WebClient)sender).OpenReadCompleted -= downloadedSong_OpenReadCompleted;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(savedFile, FileMode.Append, FileAccess.Write, myIsolatedStorage))
{
int mediaFileLength = (int)e.Result.Length;
byte[] byteFile = new byte[mediaFileLength];
e.Result.Read(byteFile, 0, byteFile.Length);
fileStream.Write(byteFile, 0, byteFile.Length);
// If there's something left, download it recursively
if (byteFile.Length > delta)
{
iterator = iterator + delta + 1;
headers = new WebHeaderCollection();
headers[HttpRequestHeader.Range] = "bytes=" + iterator.ToString() + '-' + (iterator + delta).ToString();
webClientReadCompleted.Headers = headers;
webClientReadCompleted.OpenReadCompleted += downloadedSong_OpenReadCompleted;
webClientReadCompleted.OpenReadAsync(new Uri(song.Link));
}
}
}
}
}
}
bạn đã hỏi câu hỏi này? http://stackoverflow.com/questions/14600426/how-to-download-and-save-large-1gb-video-files-from-web-into-windows-phone-8/14602104#14602104 Hãy xem tôi đáp ứng –
@Hermit, không có tôi thậm chí không nhận thấy rằng một, bởi vì tôi không có windows-phone-8 trong các thẻ yêu thích của tôi. Thật không may, câu trả lời của bạn không giúp tôi, về mặt lý thuyết tôi biết, những gì tôi nên làm :) Câu hỏi là "Làm thế nào?" – Olter
có thể điều này sẽ giúp http://stackoverflow.com/questions/5659189/how-to-split-a-large-file-into-chunks-in-c –