如何从远程 url 获取有效的文件名和扩展名来保存它?

发布于 2024-12-03 00:49:19 字数 349 浏览 1 评论 0原文

我想从远程 URL 获取实际的文件扩展名。

有时扩展名的格式无效。

例如
我从下面的URL遇到问题

1) http://tctechcrunch2011.files.wordpress.com/2011/09/media-upload.png?w=266
2) http://0.gravatar.com/avatar/a5a5ed70fa7c651aa5ec9ca8de57a4b8?s=60&d=identicon&r=G

我想从远程URL下载/保存远程图像..

如何从上面的URL获取文件名和扩展名?

谢谢
阿布舍克

I want to get actual file extension from the remote url.

sometimes extension not in valid format.

for example
I getting problem from below URLs

1) http://tctechcrunch2011.files.wordpress.com/2011/09/media-upload.png?w=266
2) http://0.gravatar.com/avatar/a5a5ed70fa7c651aa5ec9ca8de57a4b8?s=60&d=identicon&r=G

I want to download/save remote image from remote url..

How to get filename and extension from the above url?

Thanks
Abhishek

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

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

发布评论

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

评论(2

灯角 2024-12-10 00:49:19

远程服务器发送一个包含资源 mime 类型的 Content-Type 标头。例如:

Content-Type: image/png

因此您可以检查此标头的值并为您的文件选择正确的扩展名。例如:

WebRequest request = WebRequest.Create("http://0.gravatar.com/avatar/a5a5ed70fa7c651aa5ec9ca8de57a4b8?s=60&d=identicon&r=G");
using (WebResponse response = request.GetResponse())
using (Stream stream = response.GetResponseStream())
{
    string contentType = response.ContentType;
    // TODO: examine the content type and decide how to name your file
    string filename = "test.jpg";

    // Download the file
    using (Stream file = File.OpenWrite(filename))
    {
        // Remark: if the file is very big read it in chunks
        // to avoid loading it into memory
        byte[] buffer = new byte[response.ContentLength];
        stream.Read(buffer, 0, buffer.Length);
        file.Write(buffer, 0, buffer.Length);
    }
}

The remote server sends a Content-Type header containing the mime type of the resource. For example:

Content-Type: image/png

So you can examine the value of this header and pick the proper extension for your file. For example:

WebRequest request = WebRequest.Create("http://0.gravatar.com/avatar/a5a5ed70fa7c651aa5ec9ca8de57a4b8?s=60&d=identicon&r=G");
using (WebResponse response = request.GetResponse())
using (Stream stream = response.GetResponseStream())
{
    string contentType = response.ContentType;
    // TODO: examine the content type and decide how to name your file
    string filename = "test.jpg";

    // Download the file
    using (Stream file = File.OpenWrite(filename))
    {
        // Remark: if the file is very big read it in chunks
        // to avoid loading it into memory
        byte[] buffer = new byte[response.ContentLength];
        stream.Read(buffer, 0, buffer.Length);
        file.Write(buffer, 0, buffer.Length);
    }
}
黎夕旧梦 2024-12-10 00:49:19

如果您想提取来自 URL 的扩展名。

var ext = VirtualPathUtility.GetExtension(pathstring)

或者使用标头来确定内容类型。有一个 Windows API 可以将内容类型转换为扩展名(它也在注册表中),但对于 Web 应用程序来说,使用映射是有意义的。

switch(response.ContentType)
{
    case "image/jpeg":
        return ".jpeg";
    case "image/png":
        return ".png";
} 

You can use the VirtualPathUtility if you want to extract the extension from a URL.

var ext = VirtualPathUtility.GetExtension(pathstring)

Or use headers to determine the content type. There's a Windows API to convert content type to extension (it's also in registry), but for web apps it makes sense to use mapping.

switch(response.ContentType)
{
    case "image/jpeg":
        return ".jpeg";
    case "image/png":
        return ".png";
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文