从 HTTP 获取资源并保存到 Iso Storage
我目前正在使用此代码将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用以下代码
1. 使用参数调用此函数 (http://www.domain.com/media.wma)
2. 事件处理程序将媒体文件下载到独立存储内所需的 (iso_path) 位置。
}
U can use the following code
1.Call this function with parameter (http://www.domain.com/media.wma)
2.Event Handler which downloads the media file to the desired(iso_path) location inside Isolated Storage.
}
Application.GetResourceStream
获取应用程序中嵌入的资源的相对 URI。它不是 HTTP 客户端。
相反,您应该使用
WebClient
或HttpWebRequest
类。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
orHttpWebRequest
classes.