如何在Asp.Net中使用Common Response.Contentype从数据库下载任何文件? [英] How Do I Download Any File From Database With Common Response.Contentype In Asp.Net?
本文介绍了如何在Asp.Net中使用Common Response.Contentype从数据库下载任何文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从数据库下载文件,执行文件扩展名并使用if-else和不同的response.contenttype ....根据扩展名检查扩展名。
这里是我的代码:
I am downloading file from the database by carrying out the extension of the file and checking the extension with if-else and with different response.contenttype....according to extension.
here is my code:
if (ext == ".pdf")
{
Response.ContentType = "application/pdf";
}
else if (ext == ".doc" || ext == ".rtf" || ext == ".docx")
{
Response.ContentType = "Application/msword";
}
else if (ext == ".txt")
{
Response.ContentType = "text/Plain";
}
else if (ext == ".rar")
{
Response.ContentType = "Application/x-rar-compressed";
}
else if (ext == ".jpeg" || ext==".jpg")
{
Response.ContentType = "image/jpeg";
}
else
{
Response.ContentType = "Application/octetstream";
}
Response.AddHeader("Content-disposition", "attachment; filename=" + filename);
Response.BinaryWrite(byteimg);
Response.Flush();
Response.End();
}
因此,不是每次下载时根据扩展名声明response.contenttype,想要常见的response.contentype来排除所有这些/ .....请帮助..
谢谢.. :)
So, instead of declaring response.contenttype according to extension each and every time at the time of downloading, i want the common response.contentype to exclude all these one's/.....please help..
Thanks..:)
推荐答案
您想要改变整个互联网的运作方式。您可以压缩文件,从而将类型设置为zip,但是,您需要告诉浏览器内容类型是什么,因此它知道如何处理它。
You want to change how the entire internet works. You can zip the files, and thus set the type to be a zip, but otherwise, you need to tell the browser what the content type is, so it knows what to do with it.
有一般的八位字节MIME类型,但它们在你的情况下几乎没用。
你觉得必须有比使用if更好的方法吗? / else构造?您可以将扩展和MIME类型放在字典中,只需查找要发送给客户端的扩展,以使其成为适当的MIME类型。
There are generic "octet" MIME types, but they are hardly useful in your case.
Did it dawn on you that there must be a better way than using if/else constructs? You could just put the extensions and MIME types in a Dictionary and just do a lookup of the extension you're sending to the client to get it's an appropriate MIME type.
这篇关于如何在Asp.Net中使用Common Response.Contentype从数据库下载任何文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文