日期:2014-05-18  浏览次数:20497 次

gridview实现下载?
我实现了将文件上传到本地路径,然后将路径保存在gridview中,能不能通过gridview控件实现下载?下面的是我实现上传的代码。
  If (FileUpload1.HasFile) Then
  SaveFile(FileUpload1.PostedFile)
  Dim filepath As String = "d:\uploads\"
  Dim inforname As String = System.IO.Path.GetFileName(FileUpload1.FileName)
  Dim inforsize As Integer = FileUpload1.FileBytes.Length
  Dim infortime As String = DateTime.Now.ToString("yyyy-MM-dd ")
  Dim tid As String = Label1.Text
  Dim cname As String = Label1.Text
  Dim cmd As SqlCommand
  myconn = New SqlConnection("server=localhost;database=database;uid=sa;pwd=sa")
  myconn.Open()
  Dim sqlstr As String = "insert into [information] ([inforname],[inforsize],[infortime],[tid],[cname],[filepath]) VALUES (@inforname,@inforsize,@infortime,@tid,@cname,@filepath)"
  cmd = New SqlCommand(sqlstr, myconn)
  cmd.Parameters.AddWithValue("@inforname", inforname)
  cmd.Parameters.AddWithValue("@inforsize", inforsize)
  cmd.Parameters.AddWithValue("@infortime", infortime)
  cmd.Parameters.AddWithValue("@tid", tid)
  cmd.Parameters.AddWithValue("@cname", cname)
  cmd.Parameters.AddWithValue("@filepath", filepath)
  cmd.ExecuteNonQuery()
  cmd = New SqlCommand("SELECT * FROM [information] ", myconn)
  Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
  GridView1.DataBind()
  myconn.Close()
  Else
  ' Notify the user that a file was not uploaded.
  ' Before attempting to save the file, verify
  ' that the FileUpload control contains a file.
  Response.Write("<script>alert('上传文件不能为空,请重新选择上传文件!')</script>")
  End If

 Sub SaveFile(ByVal file As HttpPostedFile)
  Dim savePath As String = "d:\uploads\"
  Dim fileName As String = FileUpload1.FileName
  HyperLink5.Text = fileName
  Dim pathToCheck As String = savePath + fileName
  Dim inforsize As Integer = FileUpload1.FileBytes.Length
  If (FileUpload1.FileBytes.Length >= 10240) Then
  Response.Write("<script>alert('上传文件过大,请重新选择上传文件!')</script>")
  If (System.IO.File.Exists(pathToCheck)) Then
  Response.Write("<script>alert('文件已存在!')</script>")
  End If
  Else : Response.Write("<script>alert('上传成功!')</script>")
  End If
  savePath += fileName
  FileUpload1.SaveAs(savePath)
  End Sub

------解决方案--------------------


可以,直接a标签指定把本地资源的url就可以啊
------解决方案--------------------
不需要用按钮,用链接就可以。
ru:
这种样式的
<a href="myfile.rar">下载</a>
------解决方案--------------------
探讨

不需要用按钮,用链接就可以。
ru:
这种样式的
<a href="myfile.rar">下载</a>

------解决方案--------------------
参考:
http://www.cnblogs.com/insus/articles/1411761.html
http://www.cnblogs.com/insus/articles/2003336.html