从 HTTP 获取资源并保存到 Iso Storage

发布于 2024-12-12 01:50:09 字数 1164 浏览 0 评论 0原文

我目前正在使用此代码将 medai 保存到隔离存储中。如果媒体是本地的,这可以正常工作,但是当我尝试从 http 地址获取 medai 时,我在 URiKind 上收到错误。我已经从绝对改变了。到相对,但仍然没有骰子。

有什么建议吗?

仅供参考 - 文件名 = http://www.domain.com/media.wma

错误:相对无法创建 URI,因为“uriString”参数表示绝对 URI。

或者:预期相对 Uri,发现绝对 Uri。

private void DownloadToIsoStore(string fileName)
        {

            string ringtonePath = GetRingtonePath();

            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            // If the file already exists, no need to "download", just return
            if (isoStore.FileExists(fileName))
            {
                return;
            }

            StreamResourceInfo sr = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));

            using (BinaryReader br = new BinaryReader(sr.Stream))
            {
                // Simulate "downloading" medai file
                byte[] data = br.ReadBytes((int)sr.Stream.Length);

                // Save to local isolated storage
                SaveToIsoStore(fileName, data);
            }

        }

I am currently using this code to save medai to isolated storage. This works frine if the media is local, but when I try to get medai from an http address I get error on URiKind. I have changed from Absolute. to Relative, but still no dice.

Any suggestions?

FYI - filename = http://www.domain.com/media.wma

Error: A relative URI cannot be created because the 'uriString' parameter represents an absolute URI.

Or: Expected relative Uri, found absolute.

private void DownloadToIsoStore(string fileName)
        {

            string ringtonePath = GetRingtonePath();

            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

            // If the file already exists, no need to "download", just return
            if (isoStore.FileExists(fileName))
            {
                return;
            }

            StreamResourceInfo sr = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));

            using (BinaryReader br = new BinaryReader(sr.Stream))
            {
                // Simulate "downloading" medai file
                byte[] data = br.ReadBytes((int)sr.Stream.Length);

                // Save to local isolated storage
                SaveToIsoStore(fileName, data);
            }

        }

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

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

发布评论

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

评论(2

染墨丶若流云 2024-12-19 01:50:09

您可以使用以下代码
1. 使用参数调用此函数 (http://www.domain.com/media.wma)

    public void **GetMediaFile**(string httpPath)
    {
        WebClient wcMedia = new WebClient();
        wcMedia.OpenReadAsync(new Uri(httpPath));
        wcMedia.OpenReadCompleted += new OpenReadCompletedEventHandler(wcMedia_OpenReadCompleted);
        wcMedia.AllowReadStreamBuffering = true;

    }

2. 事件处理程序将媒体文件下载到独立存储内所需的 (iso_path) 位置。

    void wcMedia_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {       
        string iso_path="path where you want to put media file insode the isolated storage";
        var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(iso_path, System.IO.FileMode.Create, isolatedfile))
        {
            byte[] buffer = new byte[e.Result.Length];
            while (e.Result.Read(buffer, 0, buffer.Length) > 0)
            {
                stream.Write(buffer, 0, buffer.Length);
            }
        }

}

U can use the following code
1.Call this function with parameter (http://www.domain.com/media.wma)

    public void **GetMediaFile**(string httpPath)
    {
        WebClient wcMedia = new WebClient();
        wcMedia.OpenReadAsync(new Uri(httpPath));
        wcMedia.OpenReadCompleted += new OpenReadCompletedEventHandler(wcMedia_OpenReadCompleted);
        wcMedia.AllowReadStreamBuffering = true;

    }

2.Event Handler which downloads the media file to the desired(iso_path) location inside Isolated Storage.

    void wcMedia_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {       
        string iso_path="path where you want to put media file insode the isolated storage";
        var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(iso_path, System.IO.FileMode.Create, isolatedfile))
        {
            byte[] buffer = new byte[e.Result.Length];
            while (e.Result.Read(buffer, 0, buffer.Length) > 0)
            {
                stream.Write(buffer, 0, buffer.Length);
            }
        }

}

紫竹語嫣☆ 2024-12-19 01:50:09

Application.GetResourceStream 获取应用程序中嵌入的资源的相对 URI。
它不是 HTTP 客户端。

相反,您应该使用 WebClientHttpWebRequest 类。

Application.GetResourceStream takes a relative URI to a resource embedded in your application.
It isn't an HTTP client.

Instead, you should use the WebClient or HttpWebRequest classes.

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