求asp.net+vb.net 文件下载的完整代码

loveno1 2004-04-29 04:22:52
求asp.net+vb.net 文件下载的完整代码
下载的文件个数不确定,文件路径从数据库读出
希望是完整的
谢谢!
...全文
128 8 点赞 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
loveno1 2004-04-29
好的,谢谢!
  • 打赏
  • 举报
回复
Fortune2k1 2004-04-29
上面的是下载,一次只能够下载一个文件。不可能实现只弹出一次保存对话框,却保存多个文件。
  • 打赏
  • 举报
回复
Fortune2k1 2004-04-29
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-word"
' Remove the charset from the Content-Type header.
Response.Charset = "Big5"

Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFile))

'Response.WriteFile(strFile)

Dim _file As New System.IO.FileStream(strFile, IO.FileMode.Open)

Dim context(_file.Length - 1) As Byte

_file.Read(context, 0, _file.Length)
Response.AddHeader("Content-Length", context.Length.ToString())
' Write the HTML back to the browser.
Response.BinaryWrite(context)

_file.Close()
Try
File.Delete(strFile)
Catch err As Exception
Dim i As String = err.Message
End Try

' End the response.
Response.End()
  • 打赏
  • 举报
回复
loveno1 2004-04-29
楼上发的是上传的代码吧

可我要的是文件下载的,正好反了,还有谁知道啊,谢谢!
  • 打赏
  • 举报
回复
Fortune2k1 2004-04-29
UPLOAD.aspx.vb:
Public Class UPLOAD
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File2 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File3 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File4 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents File5 As System.Web.UI.HtmlControls.HtmlInputFile

#End Region


Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Me.IsPostBack) Then Me.SaveImages()
End Sub

Private Function SaveImages() As System.Boolean

'//loop through the files uploaded

Dim _files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files

'//Message to the user
Dim _message As New System.Text.StringBuilder("Files Uploaded:<br>")
Dim _iFile As System.Int32
Try

For _iFile = 0 To _files.Count - 1


'// Check to make sure the uploaded file is a jpg or gif

Dim _postedFile As System.Web.HttpPostedFile = _files(_iFile)
Dim _fileName, _fileExtension As System.String

_fileName = System.IO.Path.GetFileName( _
_postedFile.FileName)

_fileExtension = System.IO.Path.GetExtension( _
_fileName)

If (_fileExtension = ".gif") Then


'//Save File to the proper directory
_postedFile.SaveAs( _
System.Web.HttpContext.Current.Request.MapPath( _
"gifs/") + _fileName)
_message.Append(_fileName + "<BR>")


ElseIf (_fileExtension = ".jpg") Then

'//Save File to the proper directory
_postedFile.SaveAs( _
System.Web.HttpContext.Current.Request.MapPath( _
"jpgs/") + _fileName)
_message.Append(_fileName + "<BR>")


Else

_message.Append(_fileName & " <font color=""red"">failed!! Only .gif and .jpg images allowed!</font> <BR>")

End If

Next

Label1.Text = _message.ToString()
Return True



Catch Ex As System.Exception


Label1.Text = Ex.Message
Return False

End Try



End Function

End Class
  • 打赏
  • 举报
回复
Fortune2k1 2004-04-29
UPLOAD.aspx:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="UPLOAD.aspx.vb" Inherits="HowTosVB.UPLOAD"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>::: UPLOAD SAMPLE ::: </title>
</HEAD>
<body>
<center>
<form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
<h3>Multiple File Upload Example</h3>
<P>
<INPUT type="file" runat="server" size="50" ID="File1" NAME="File1"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File2" NAME="File2"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File3" NAME="File3"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File4" NAME="File4"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File5" NAME="File5"></P>
<P><STRONG>::  </STRONG>
<asp:LinkButton id="LinkButton1" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small">Upload Images</asp:LinkButton>  <STRONG>::
</STRONG>  <A href="JavaScript:document.forms[0].reset()" id="LinkButton2" style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana">
Reset Form</A> <STRONG>::</STRONG></P>
<P>
<asp:Label id="Label1" runat="server" Font-Names="verdana" Font-Bold="True" Font-Size="XX-Small" Width="400px" BorderStyle="None" BorderColor="White"></asp:Label></P>
<P> </P>
</form>
</center>
</body>
</HTML>
  • 打赏
  • 举报
回复
loveno1 2004-04-29
没人知道吗?
  • 打赏
  • 举报
回复
loveno1 2004-04-29
自己顶一下
  • 打赏
  • 举报
回复
相关推荐
发帖
.NET社区
加入

5.9w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-04-29 04:22
社区公告
暂无公告