使用Response.TransmitFile时无法更改标签文本? [英] can't change label text when use Response.TransmitFile ?

查看:93
本文介绍了使用Response.TransmitFile时无法更改标签文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试浏览文件并上传到文件夹然后下载它

从同一个按钮上传和下载

< pre lang =c#> string fileSavePath = Server.MapPath( 〜/上传/);
FileUpload1.PostedFile.SaveAs(fileSavePath + fileName);
File.Copy(fileSavePath + fileName,Server.MapPath( 〜/ processed / )+ fileName, true );
// 响应部分
Response.ContentType = application / zip;
Response.AppendHeader( Content-Disposition attachment; filename = + fileName);
Response.TransmitFile(Server.MapPath( 〜/ processed / + fileName)) ;
Response.End();
//
Label1.Text = 文件已成功上传;



然后我想写成功上传文件标签

标签文字永远不会更改为文件成功上传

当我评论响应部分文本更改成功

为什么使用时响应永远不会写在页面上?

谢谢..

解决方案

这不是导致这种情况的Response.TransmitFile:它是Response.End

这将终止流并将其发送到客户端,因此忽略之后执行的任何操作。



事实上,MS不建议使用Response.End: https://support.microsoft.com/en-us/kb/312629 [ ^ ]


当您提交表单并发出请求时,您的asp.net代码正在发送响应。当你正在做一个普通的页面时,asp.net会从你的控件生成html并将该响应写成text\html,这就是响应,它出现在浏览器中。当您将自己的内容类型设置为zip文件并使用transmitfile时,您将从asp.net接管并发送您自己的自定义响应,因此提供浏览器以下载该文件。一个请求只能有一个响应....你不能发送文件*和*发送HTML。如果你想要一个请求有两个响应(下载和html),那么你需要使用一些技巧。



一种常见的技巧是将你的页面发送为与您的文本一样正常,并在该页面中放置一个隐藏的iframe,其中src指向执行transmitfile的页面。这样他们就会看到消息,然后得到文件下载提示。



 < ;   iframe     src  < span class =code-keyword> =  download.aspx?file = filenametodownload    width   =  1    height   =  1    style   =   display:none; >  < ;   / iframe  >  





您的download.aspx将读取文件名并发起传输文件。


Hi,
I am trying to browse file and upload to folder then download it
upload and download from the same button

string fileSavePath = Server.MapPath("~/uploads/");
FileUpload1.PostedFile.SaveAs(fileSavePath + fileName);
File.Copy(fileSavePath + fileName, Server.MapPath("~/processed/") + fileName, true);
//Response section
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.TransmitFile(Server.MapPath("~/processed/" + fileName));
Response.End();
//
Label1.Text = "File successfully uploaded";


then I want to write File successfully uploaded in the label
label text never change to File successfully uploaded
when I comment Response section text change successfully
why when use Response never write on page ?
Thanks..

解决方案

It's not the Response.TransmitFile that causes this: it's the Response.End
This terminates the stream and sends it to the client, so anything you do after that is ignored.

In fact, MS don't recommend Response.End anyway: https://support.microsoft.com/en-us/kb/312629[^]


When you submit the form and instigate the request what your asp.net code is doing is sending a response. When you're doing a normal page, asp.net will generate html from your controls and write that response as text\html and that's the response, it appears in the browser. When you set your own content type as a zip file and use transmitfile then you're taking over from asp.net and sending your own custom response, so the browser is offered to download the file. A request can only have one response....you can't send a file *and* send html. If you want one request to have two responses (a download and html) then you'll need to employ some trickery.

A common technique is to send your page as normal with your text, and in that page put a hidden iframe with the src pointing to a page that does the transmitfile. That way they see the message, and then get the file download prompt.

<iframe src="download.aspx?file=filenametodownload" width="1" height="1" style="display:none;"></iframe>



Your download.aspx will then read the filename and instigate the transmitfile.


这篇关于使用Response.TransmitFile时无法更改标签文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆