使用Response.TransmitFile下载文件,但也包含了页面的源代码 [英] Using Response.TransmitFile to download file but also contains the page source
问题描述
下面是违规code,我下载一个CSV但它附加的网页的源文件的底部如何prevent任何想法?
VAR价格表=测试();
常量字符串downloadName =PriceList.csv;
变种FS =新的FileStream(downloadName,FileMode.Create); VAR CSV =新CsvHelper.CsvHelper(FS);
csv.Writer.WriteRecords(价格表);
Response.ClearContent(); //不知道正确的内容类型是什么。这是probally错误
Response.ContentType =应用程序/ XLS
//设置大小可选
Response.AddHeader(内容处置,
附件;文件名=+ downloadName +;大小=+ fs.Length.ToString());
变种FN = fs.Name;
fs.Close();
loadingImage.Visible = FALSE;
Response.TransmitFile(FN);
Response.Flush();
呼叫到Response.End()
。
另外,为什么保存该文件只是重新发送它?充其量,这是浪费,而且如果你打算重用的名字,那么你已经一个竞争条件,如果两个人打这个页面在同一时间。相反,发送文件,请使用 VAR CSV =新CsvHelper.CsvHelper(Response.OutputStream)
让你写直奔浏览器(你必须先虽然发送您的头)。
此外,内容类型为CSV文件,为文本/ CSV
。
Below is the offending code, I download a csv however it appends the page source to the bottom any ideas on how to prevent this?
var priceList = Test();
const string downloadName = "PriceList.csv";
var fs = new FileStream(downloadName, FileMode.Create);
var csv = new CsvHelper.CsvHelper(fs);
csv.Writer.WriteRecords(priceList);
Response.ClearContent();
//not sure what the correct content type is. This is probally wrong
Response.ContentType = "application/xls";
//Setting size is optional
Response.AddHeader("Content-Disposition",
"attachment; filename=" + downloadName + "; size=" + fs.Length.ToString());
var fn = fs.Name;
fs.Close();
loadingImage.Visible = false;
Response.TransmitFile(fn);
Response.Flush();
Call Response.End()
.
Also, why save the file just to resend it? At best this is wasteful, but also if you're reusing the name then you've a race-condition if two people hit this page at the same time. Instead of sending the file, use var csv = new CsvHelper.CsvHelper(Response.OutputStream)
so you write straight to the browser (you'll have to send your headers first though).
Also, the content-type for CSV files is text/csv
.
这篇关于使用Response.TransmitFile下载文件,但也包含了页面的源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!