C# 如何从远程url获取有效的文件名和扩展名以进行保存。?
我想从远程url获取实际的文件扩展名 有时扩展名格式无效 例如,我从下面的URL得到问题C# 如何从远程url获取有效的文件名和扩展名以进行保存。?,c#,asp.net,c#-2.0,C#,Asp.net,C# 2.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获取文件名和
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获取文件名和扩展名
谢谢
Abhishek远程服务器发送一个包含资源mime类型的内容类型头。例如:
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);
}
}
因此,您可以检查此标头的值,并为文件选择适当的扩展名。例如:
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);
}
}
远程服务器发送包含资源mime类型的内容类型标头。例如:
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);
}
}
因此,您可以检查此标头的值,并为文件选择适当的扩展名。例如:
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);
}
}
如果要从URL提取扩展名,可以使用
var ext = VirtualPathUtility.GetExtension(pathstring)
或者使用标题确定内容类型。有一个Windows API可以将内容类型转换为扩展,它也在注册表中,但对于web应用程序来说,使用映射是有意义的
switch(response.ContentType)
{
case "image/jpeg":
return ".jpeg";
case "image/png":
return ".png";
}
如果要从URL提取扩展名,可以使用
var ext = VirtualPathUtility.GetExtension(pathstring)
或者使用标题确定内容类型。有一个Windows API可以将内容类型转换为扩展,它也在注册表中,但对于web应用程序来说,使用映射是有意义的
switch(response.ContentType)
{
case "image/jpeg":
return ".jpeg";
case "image/png":
return ".png";
}
当我们发送web请求时如何获取内容类型?您不能。毕竟,在服务器实际发送之前,您如何预测它将向您发送什么?一旦您有了WebResponse,您就可以得到它的内容类型。@Abhishek Bhalani,内容类型标题在响应上,而不是在请求上。我已经展示了一个例子。@Darin谢谢,但是在webrequest之后我怎么能下载文件呢?@Abhishek Bhalani,当然可以。该文件位于响应流中。只需阅读此流。我更新了我的答案,用一个例子来说明。当我们发送web请求时,我们如何获得内容类型?你不能。毕竟,在服务器实际发送之前,您如何预测它将向您发送什么?一旦您有了WebResponse,您就可以得到它的内容类型。@Abhishek Bhalani,内容类型标题在响应上,而不是在请求上。我已经展示了一个例子。@Darin谢谢,但是在webrequest之后我怎么能下载文件呢?@Abhishek Bhalani,当然可以。该文件位于响应流中。只需阅读此流。我更新了我的答案,用一个例子来说明。我尝试了URL:然后它抛出异常..是的,它使用字符串的方式与文件系统路径的路径相同。如果它没有扩展名,它将无法工作,您将需要内容类型方法。我尝试了URL:然后它抛出异常..是的,它使用字符串的方式与文件系统路径的路径相同。如果没有扩展,它将无法工作,您将需要内容类型方法。