我有一个简单的ASP.NET网页,使用VB和文件上传控件。用户将上传.xls,并在项目目录中使用新格式和一些计算创建新的.xls文件。此文件保存,打开和显示没有问题。
在此按钮单击事件中,我希望新创建的文件下载到用户的浏览器/下载文件夹。当Response.WriteFile与Response.End一起使用时,.xls文件正确下载...但是有一个线程中止异常被捕获,并且在处理Response.WriteFile行之后没有任何内容,即使我把它放在catch语句中(即我有一个标签,我想告诉用户然后显示该文件已成功下载。
以下是最常用的初始代码:
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
If fuReport.HasFile Then
'All report processing occurs & report is created without issue
Try
Response.Clear()
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", strReportName))
Response.ContentType = "application/vnd.ms-excel"
'Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.WriteFile(strReportName)
Response.End()
lblResult.Text = "Report was successfully downloaded."
Catch
lblResult.Text = "Testing1."
Finally
lblResult.Text = "Testing2."
fuReport = New FileUpload()
End Try
End If
End Sub
我已经看到了针对.xls文件的ContentType的一些不同建议。上面的两种内容类型都将下载该文件,但不处理Catch或Finally语句中的任何内容。 lblResult仍然会写道:“请等待你的报告处理......”
我在网上发现问题在于Response.End()。根据{{3}},我将其替换为
HttpContext.Current.ApplicationInstance.CompleteRequest()
这引起了一个新问题。我的.xls文件会下载,但它似乎显示乱码,其次是一些aspx页面内容。此处,即使标签未在网页上更改,也会正确列出标签。我没有足够的声誉来包含屏幕截图,但这是Excel文件显示的内容之一:
ˆdQ_9€`Èb/ecÈ¢l†=gÊC\Œ¬ÀxbY”Â…‘#ˆ,ör6ˆ,Ê…8ëY8ÖæBô?³2º‹CÓž€|^î¯{{<xJñ¯ªç2,FÑ+sbŸ£å›q³žcm"9ŸÛÆ…îâpoŸûñä_õ¥1K×Ê1Ü–‘äŒä‚$‹’,ÊÞÿ?ëY9ÖÎɺa 9†7(Æ‘E‰c<µŠ$wïå¼Q9çƒìù93vì£Þ¾ŠõXè.î·ÛúÏöX;`>¸w!‚™•ó‘#Ž,8²(ãÞqÖ3q¬M¥dÝ ‚Â7VE¾‰`|mä^@Y”°çÞÌØZXß²‹O•½në›×å=än½‘°i”EàqÅ^Ê6‹²(×›õìks žú¦¸Ð½µW§îo[‚‡ÚGÈýz3·!dÑ„@ dq€E9õ×3lfl%Ø™^Vv
@ÈB{)[²èïmÖ³l¬…8óI–½-?o{ÖΤ,÷IJhC ²ØKÙ²hmïÕΞâ}û‡.vÝÌznµ³ð±Lâc¹7†E ÜOpÁÅ^ÎÆEyüæ=£ÆÚÄÏnïýúº·7åå‡v³Í[;οŠþ•PˆÃp%*0B!ö
ÑÓè¼gÓX›8N—ï\èõXèÞÞ4·íTÏöõeùí$ñõ~À“Böº/kûϵýõ\Þ8½>SF+/åê1³{þÌœý†éêÔߊ*t-Áwí>ÕnW¿¯ö΀žE” 9åÕÈFY”ùöâ¼gÏX›*Ñ^¸ñÓ*‘uo›ËK;”Áð/.
½g/D‰ ?KQFþÝF3©ö|š9›1JÖi”¬÷m6‘lB/oD‘s0²èŒ=·fÎvBŒ‘u#ëŒ,JãK#YPdÑ1{ŽÍœ{+<˜WY÷v_ï®Úǽªêë{ñæѽЖ9ÅëÚ–9,À’–,X²(×z›÷ÌkÓÒö;+»>²ì6£®ËÛwÁýë½á0‚,JaRå!‹½œm¥Ê¢×vôúÁìÉï¼éŸ÷<kÁé*øLƒÐýËNÖ|SloŸmë¿ìÍ©ƒ·ú·Å*÷ï½Ðl(Y”PÆ+$YdQv1öü›9‹qT¬kQþk[W‡;+õ…›âš"z÷FÅ@r× ¤X³rA“Å^ÞV—,:â2¹è:Ö¦Úœ-fþô*t”×Õ·í»©ÁÛþ"„¢D3,K€A
±RˆòMYôÌkç1dÝ †"`È¢Ä0¾JŠ_, r÷DeBìù9vìÌTS±ÐýQnm‹x;©îªƒ³P:‹^ùJ”ÆeÈ?Wdq@EÙïN-zfŽµ±Cˆ¬‘xV AdQ‚uÈEY”O±çã,Øf°ÃyE±ÝU}gÞøÛò]mŸ;ök‘{öWª¢Û„1®EÑë“Éé“ñ©ûRÃsõ£ŠÜcælÚópì1ÄY7!Gð}U‘nB•ÈÝŠJdqÀE™{΂½…˜!ë†0äC%†|Ÿ!~´àÈ)Y”ɱgà,Ø`˜¬‚…
Ëþ¨¶—;oì‰q°óFtì
‡]¹Û„1žM9€ Èb/åJ9cóÔ¢çÞX×4‹¹wY²«!ëþ¨+û4Š8¿z*"xƒaü¸ÛĦRŽ ²ØËÙ²({ø¢çÜX›0Ž‚³n…ì;V£.oǧkŠ Þ HN8Œ‘Ž,öR6Ž,Ê›J—=·ÆÚqí$:£xÛÞ$Ú‹R‡ÏîÂTðŸM Q"ÈWD…1
±Qˆr?8µìY6ÖÎɺ 9€7 ÅXd›@Æsªˆ ~Εs6Äže³dG!®FÖ
€Èˆ,JÃùTü\Á{÷R6†,ÊfØsl–ì'Ì#Ï;Y…ÎÚ5ñäo'›}]í.Gßú“+çà›¨"8¹W“ÅNñDcÙso¬ÍSkD”ußG”cyÃc³,‹Q˜e9‚@Êb/g«Pƒ´çæ,Ùj°"õß¾º?ê݇v+ÇoÍv,{¸coLŒ#‹†pä‚#‹½œ#‹ŽáØst–l5Ìã`²eÝÍÎοY—ûwöBŽ?ÃrßÐÙ&ŽbùÊ]–,X²(oëñ²gèX›gØàb¡û£i®Ì/ªw;ôwrˆGN7q„y•#†,²(w+DzçëX›).¼…‡|X÷ÇÞöÅÙ!Föjc|ûFDðFÄfUî6QŒ—¯@@d±—²Mª,:fRí;K6숿€#ë†päÞ G%ŽPA€d±—³dÑ W=sÇÚ\sÏfY…î¿ÛÅ÷íƒÇÆM\轿žBuš0†å(zeŠBìS¢c(öœ›¶ïØ·Ë…î¿Û×S‹ªjÿÏ]â}@‘“@‘Š,(²(Ïc]õ\kc†Y§²Þ
+C%€¼FQENÁË{£òΤØóuVì5ÄeÈ:M‘õÞhE%Šñ5QD¹{/g#È"$øäEy(ýùãMc;ÓêË×û“÷Íîð«_~°ëàáóÇê—vÍY³ûÓü3Û}þÃûomWi{˜[½»;ÙVïöŸþÔnxXrþçš¿ü`“Ä;ûä[sûм©ì½õ}ûØü¾ilGÜ—ÿa1¿ü»çÿð¤ÜÖ×»?l{Î×^ì¿jû}ò©ÙxÈù×ÿÿÿPK-!®ÄñQ"[Content_Types].xmlPK-!ùJ¬öW
sxl/styles.xmlPK-!é¦%¸‚Sü xl/theme/theme1.xmlPK-!òñ%>e¯docProps/core.xmlPK-! pUŠ¡5$docProps/app.xmlPK-!Š¥aú`qûxl/workbook.xmlPK-!'¡Œ=Å5߈xl/worksheets/sheet1.xmlPK
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"><title>
</title>
<script type="text/javascript">
function changeLabel(){
document.getElementById("lblResult").innerHTML = "Please wait while your report is processed...";
}
</script>
</head>
<body>
<form method="post" action="Uploader.aspx" id="form1" enctype="multipart/form-data">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="+yve+0TnTzr8dZbqET3gRkZx/Pw9iGFmDqDyyfeTPMoXlPTPVgX3FrRUhTd3lBZE0DkzyUKzhXOi6MqTdxe1jnYL+mdzQiyoaM5w5cv9vij3X1xtsW8P1zACrDj6IqUK87n3GcwFtdHCCblAdgFI8nPQZsJnRv9w8lNIurcrbfad534ICOt8S/++vRi9+s91DFi3woIbFMyxw3rvbUPQwg==" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="C153B6F1" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="c3fKPUwILazJv1aW60quCxYtkspAHgetjdwMb7ugaEBN7qnUTTL7SrAVizVMaqXPAPqmyhDV0rtT1upkWYH9eOzqHjjmjvr9ZHty08BHB9OD/fCIuF8ExZbOqpiXOdGA" />
</div>
<div>
<input type="file" name="fuReport" id="fuReport" style="width:306px;" />
<br />
<span id="lblResult">Report was successfully downloaded.</span>
<br />
<input type="submit" name="btnUpload" value="Upload" onclick="changeLabel();" id="btnUpload" />
</div>
</form>
</body>
</html>
然后我尝试用HTTPContext替换所有的响应代码,如下所示:
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", strReportName))
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.Write(strReportName)
HttpContext.Current.ApplicationInstance.CompleteRequest()
这也会下载.xls文件,但会显示网页的图形表示。再次,我无法发布图片,但Excel文件打开以显示带有文本的按钮,上传和放大表示:
报告已成功下载。
我已经研究了这个没有解决方案的HOURS。我尝试将这个特定的代码从try / catch / finally中拉出来,因为其他人已经取得了成功,但这也无济于事。
有没有人知道我如何正确地让浏览器下载.xls文件和我的NewReport的实际内容而不是它包含来自aspx页面的信息?
答案 0 :(得分:0)
I like to use BinaryWrite, something like this:
Dim ms As New IO.MemoryStream
xWB.Write(ms)
Dim bytes As Byte() = ms.ToArray
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=" & strReportName)
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
答案 1 :(得分:0)
我放弃了自动执行这两个过程的想法。我添加了一个“下载”按钮,该按钮仅在成功处理报告后才会显示。在点击事件的下载按钮上,我有excel文件下载。这比我想要的还要多一点,但它完成了工作。
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
If fuReport.HasFile Then
Try
'All report processing occurs & report is created without issue
lblResult.Text = "Report was successfully processed."
btnDownload.Visible = True
Catch
lblResult.Text = "An error occurred. Please try again."
Finally
fuReport = New FileUpload()
End Try
Else
lblResult.Text = "Please select a report to upload."
End If
End Sub
Protected Sub btnDownload_Click(sender As Object, e As EventArgs) Handles btnDownload.Click
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", strReportName))
Response.ContentType = "application/vnd.ms-excel"
Response.WriteFile(strReportName)
Response.End()
End Sub
End Class