如何在 Windows Phone 7 中以编程方式检索下载的文件?

发布于 2024-11-27 16:46:40 字数 484 浏览 1 评论 0原文

我正在网上下载 epub 文件。为此,我首先使用 Directory.CreateDirectory 创建了一个目录,然后使用以下代码下载了文件。

WebClient webClient = new WebClient();
webClient.DownloadStringAsync(new Uri(downloadedURL), directoryName);
webClient.DownloadProgressChanged += 
                   new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadStringCompleted += 
                   new DownloadStringCompletedEventHandler(Completed);

这是下载文件的正确方法吗?查看下载的文件并将其显示在网格中的代码是什么?

I am downloading an epub file online. For this I first created a directory using Directory.CreateDirectory, then downloaded the file using following code.

WebClient webClient = new WebClient();
webClient.DownloadStringAsync(new Uri(downloadedURL), directoryName);
webClient.DownloadProgressChanged += 
                   new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadStringCompleted += 
                   new DownloadStringCompletedEventHandler(Completed);

Is this the correct way of downloading files? What is the code to view the file which is downloaded and displaying it in a grid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2024-12-04 16:46:40

1) 您不应在 Windows Phone 上使用 Directory.CreateDirectory。相反,由于您在独立存储上操作,因此需要使用:

var file = IsolatedStorageFile.GetUserStoreForApplication();
file.CreateDirectory("myDirectory");

2) 下载文件可以通过 WebClient 完成:

WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("your_URL"));


void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    var file = IsolatedStorageFile.GetUserStoreForApplication();

    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("file.epub", System.IO.FileMode.Create, file))
    {
        byte[] buffer = new byte[1024];
        while (e.Result.Read(buffer, 0, buffer.Length) > 0)
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}

在这种情况下直接创建目录是可选的。如果您需要将文件保存在嵌套的文件夹结构中,不妨将文件路径设置为/Folder/NewFolder/file.epub之类的内容。

3) 要枚举独立存储中的文件,您可以使用:

var file = IsolatedStorageFile.GetUserStoreForApplication();
file.GetFileNames();

如果文件位于 IsoStore 的根目录中。如果它们位于目录内,则必须设置搜索模式并将其传递给 GetFileNames - 包括文件夹名称和文件类型。对于每个文件,您可以使用以下模式:

DIRECTORY_NAME\*.*

1) You should not use Directory.CreateDirectory on Windows Phone. Instead, since you are operating on Isolated Storage, you need to use:

var file = IsolatedStorageFile.GetUserStoreForApplication();
file.CreateDirectory("myDirectory");

2) Downloading files can be done through WebClient this way:

WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri("your_URL"));


void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    var file = IsolatedStorageFile.GetUserStoreForApplication();

    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("file.epub", System.IO.FileMode.Create, file))
    {
        byte[] buffer = new byte[1024];
        while (e.Result.Read(buffer, 0, buffer.Length) > 0)
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}

Creating a directory directly in this case is optional. If you need to save the file in a nested folder structure, you might as well set the file path to something like /Folder/NewFolder/file.epub.

3) To enumerate files in the Isolated Storage, you could use:

var file = IsolatedStorageFile.GetUserStoreForApplication();
file.GetFileNames();

That's if the files are located in the root of the IsoStore. If those are located inside a directory, you will have to set a search pattern and pass it to GetFileNames - including the folder name and file type. For every single file, you could use this pattern:

DIRECTORY_NAME\*.*
贪了杯 2024-12-04 16:46:40

没有文件。 DownloadStringCompleted 事件参数包含一个 Result 成员,该成员包含一个字符串,该字符串是 HTTP 请求的结果。在事件处理程序中,您可以将其称为 e.Result

我不熟悉 epub 文件的格式,但除非它们是严格的文本文件,否则您的代码无法正常工作。

您应该改用 webclient.OpenReadAsync 和相应的事件,其方法与 DownloadStringAsync 类似,只不过 e.Result 是可用于读取二进制数据的流。

There is no file. The The DownloadStringCompleted event argument contains a Result member containing a string that is the result of your HTTP request. In the event handler you can refer to this as e.Result

I am unfamiliar with the format of epub files but unless they are strictly text files your code cannot work properly.

You should instead use webclient.OpenReadAsync and the corresponding event, which is similar to DownloadStringAsync in methodology, except that e.Result is a stream you can use to read binary data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文