从按钮单击下载 [英] Download from button click

查看:72
本文介绍了从按钮单击下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让服务器从

按钮点击和文件名字符串发送到客户端(即下载)?

Is there a way to have the server send a file to client (ie download) from a
button click and a filename string?

推荐答案

" John Smith Jr." < me@isp.com>在留言中写道

新闻:ON ************** @ TK2MSFTNGP12.phx.gbl ...
"John Smith Jr." <me@isp.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
有没有办法让服务器从
a按钮点击和文件名字符串向客户端发送文件(即下载)?
Is there a way to have the server send a file to client (ie download) from a button click and a filename string?




是的。如果您愿意,甚至可以使用客户端代码完成,使用

< input type ="按钮"

onclick =" javascript < /b>:window.location.href=''MyDownlo ad.pdf''/>表单类型

元素。


如果你想在服务器上流式传输它,你可以打开一个FileStream并发布

生成的二进制数据到Response.OutputStream


R.



Yes. And it can even be done with client side code if you want, using an
<input type="button"
onclick="javascript:window.location.href=''MyDownlo ad.pdf'' /> type of form
element.

if you want to stream it on the server, you can open a FileStream and post
the resulting binary data to the Response.OutputStream

R.


嗨约翰,


如果你想要一个服务器端方法,试试这个?


Private Sub LinkBut​​ton1_Click _

(ByVal sender As System.Object,_

ByVal e As System.EventArgs)_

处理LinkBut​​ton1.Click

Dim FilePath As String =" C:\downloads \ setup。 exe"

Dim FileName As String =" setup.exe"

Response.ContentType =" application / octet-stream"

Response.AddHeader(" Content-Disposition",_

" attachment; filename ="& FileName)

''将文件直接写入HTTP输出流。

Response.WriteFile(FilePath)

Re sponser.End()

End Sub

< form id =" Form1"方法= QUOT;交" runat =" server">

< asp:LinkBut​​ton id =" LinkBut​​ton1" runat =" server">点击

下载< / asp:LinkBut​​ton>

< / form>

" John Smith小" < me@isp.com>在留言中写道

新闻:ON ************** @ TK2MSFTNGP12.phx.gbl ...
Hi John,

If you want a server-side method, try this?

Private Sub LinkButton1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles LinkButton1.Click
Dim FilePath As String = "C:\downloads\setup.exe"
Dim FileName As String = "setup.exe"
''Set the appropriate ContentType.
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=" & FileName)
''Write the file directly to the HTTP output stream.
Response.WriteFile(FilePath)
Response.End()
End Sub
<form id="Form1" method="post" runat="server">
<asp:LinkButton id="LinkButton1" runat="server">Click to
download</asp:LinkButton>
</form>
"John Smith Jr." <me@isp.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
有没有办法让服务器从
a按钮点击和文件名字符串向客户端发送文件(即下载)?
Is there a way to have the server send a file to client (ie download) from a button click and a filename string?





非常感谢。


必须将其转换为C#,但这很简单。


A但是很少有问题,在开始下载之前有一个暂停,这不是很长但是它是本地局域网,但不是什么大问题。但似乎

使用默认作为文件名,无论如何都要设置目的地

文件名默认为什么?

" Ken Cox [Microsoft MVP]" < BA ************ @ sympatico.ca>在消息中写道

新闻:eO ************** @ TK2MSFTNGP12.phx.gbl ...
That worked great!
Thank you much.

Had to convert it to C#, but thats pretty easy.

A few questions though, there is a pause before it starts downloading, which
isn''t to long but it is local lan, not a big deal though. But it seems to
use "default" as a file name, is there anyway to set what the destination
file name should be by default?
"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:eO**************@TK2MSFTNGP12.phx.gbl...
嗨约翰,

如果你想要一个服务器端方法,试试这个吗?

私有子LinkBut​​ton1_Click _
(ByVal发送者As System.Object,_
ByVal e As System.EventArgs)_
处理LinkBut​​ton1.Click
Dim FilePath As String =" C:\ downloader \ setup.exe"
Dim FileName As String =" setup.exe"
''设置适当的ContentType。
Response.ContentType =" application / octet-stream"
Response.AddHeader(" Content-Disposition",_
" attachment) ; filename ="& FileName)
''将文件直接写入HTTP输出流。
Response.WriteFile(FilePath)
Response.End()
End Sub

< form id =" Form1"方法= QUOT;交" runat =" server">
< asp:LinkBut​​ton id =" LinkBut​​ton1" runat =" server">点击下载< / asp:LinkBut​​ton>
< / form>

" John Smith Jr." < me@isp.com>在消息中写道
新闻:ON ************** @ TK2MSFTNGP12.phx.gbl ...
Hi John,

If you want a server-side method, try this?

Private Sub LinkButton1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles LinkButton1.Click
Dim FilePath As String = "C:\downloads\setup.exe"
Dim FileName As String = "setup.exe"
''Set the appropriate ContentType.
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=" & FileName)
''Write the file directly to the HTTP output stream.
Response.WriteFile(FilePath)
Response.End()
End Sub
<form id="Form1" method="post" runat="server">
<asp:LinkButton id="LinkButton1" runat="server">Click to
download</asp:LinkButton>
</form>
"John Smith Jr." <me@isp.com> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
有没有办法让服务器发送一个文件到客户端(即下载)
Is there a way to have the server send a file to client (ie download)


来自

按钮点击和文件名字符串?
button click and a filename string?




这篇关于从按钮单击下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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