我想将zip文件直接保存到服务器的隔离存储中,但我遇到的问题是当我尝试使用下面的代码进行保存时,由于我的文件大小为>有时150 MB。所以我发布了一个问题here,建议是
你可以将这样的文件直接下载到IsolatedStorage,但是如果你想把它放到内存中 - 可能会有问题。
那么如何将文件从服务器直接保存到隔离存储而不保存到内存。 我使用的代码发布在这里
client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(fileurl), objRef);
private async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var file = IsolatedStorageFile.GetUserStoreForApplication();
if (!file.DirectoryExists("Folderpath/Files"))
{
file.CreateDirectory("Folderpath/Files");
}
string hpubFile = "/Folderpath/Files/fileName" ;
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(hpubFile, System.IO.FileMode.OpenOrCreate, FileAccess.ReadWrite, file))
{
byte[] buffer = new byte[1024];
while (e.Result.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
}
}
}
答案 0 :(得分:0)
然后尝试这样的方法 - 用HttpWebRequest做到这一点:
public static class Extensions
{
public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest webRequest)
{
TaskCompletionSource<HttpWebResponse> taskComplete = new TaskCompletionSource<HttpWebResponse>();
webRequest.BeginGetResponse(
asyncResponse =>
{
try
{
HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
taskComplete.TrySetResult(someResponse);
}
catch (WebException webExc)
{
HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
taskComplete.TrySetResult(failedResponse);
}
catch (Exception exc) { taskComplete.SetException(exc); }
}, webRequest);
return taskComplete.Task;
}
}
CancellationTokenSource cts = new CancellationTokenSource();
enum Problem { Ok, Cancelled, Other };
public async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName, CancellationToken cToken)
{
try
{
HttpWebRequest wbr = WebRequest.CreateHttp(uriToDownload);
wbr.Method = "GET";
wbr.AllowReadStreamBuffering = false;
WebClient wbc = new WebClient();
using (HttpWebResponse response = await wbr.GetResponseAsync())
{
using (Stream mystr = response.GetResponseStream())
{
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
{
if (ISF.FileExists(fileName)) return Problem.Other;
using (IsolatedStorageFileStream file = ISF.CreateFile(fileName))
{
const int BUFFER_SIZE = 100 * 1024;
byte[] buf = new byte[BUFFER_SIZE];
int bytesread = 0;
while ((bytesread = await mystr.ReadAsync(buf, 0, BUFFER_SIZE)) > 0)
{
cToken.ThrowIfCancellationRequested();
file.Write(buf, 0, bytesread);
}
}
}
return Problem.Ok;
}
}
}
catch (Exception exc)
{
if (exc is OperationCanceledException)
return Problem.Cancelled;
else return Problem.Other;
}
}
private async void Downlaod_Click(object sender, RoutedEventArgs e)
{
cts = new CancellationTokenSource();
Problem fileDownloaded = await DownloadFileFromWeb(new Uri(@"http://filedress/myfile.txt", UriKind.Absolute), "myfile.txt", cts.Token);
switch(fileDownloaded)
{
case Problem.Ok:
MessageBox.Show("Problem with download");
break;
case Problem.Cancelled:
MessageBox.Show("Download cancelled");
break;
case Problem.Other:
default:
MessageBox.Show("Other problem with download");
break;
}
}
因为我有我的WebResponse,我从中获取一个流然后将其读取为异步。
请注意,我没有对上面的代码进行过测试,但希望它可以为您提供一些如何工作的建议。
答案 1 :(得分:0)
是的,您可以这样做,就像首先在Sqlite文件中保存原始数据并在代码中使用,然后调用服务器进行数据中的更改并获取这些更新,这样您就不必再次下载数据了然后再次.. 此外,如果您必须在每次调用时下载数据,则可以使用sqlite或linq。