ورود

View Full Version : سوال: download فایل



نسیم 96
دوشنبه 25 آذر 1387, 01:30 صبح
با سلام
سوالم رو خیلی سرچ کردم . تو همین جا هم سوال چند نفر بود ولی پاسخشون خیلی مختصر بود. سوال من اینه:
من یه سری فایل مثلا pdf و... توی یه شاخه مثلا c:\uploads دارم. چه طوری باید امکان دانلود اونا رو به کاربرام بدم(with vb.net )خیلی ممنونم.

r_mehrizi
سه شنبه 26 آذر 1387, 09:22 صبح
اين نمونه كدي است كه براي دانلود فايل استفاده ميشه


<asp:TemplateField HeaderText="دریافت فایل">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/Images/download.gif" AlternateText="دریافت فایل مزایده" CommandName="Download" CommandArgument="" Width="30" Height="30" ID="lnkpro2" runat="server" />
</ItemTemplate>
</asp:TemplateField>





Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles GridView1.RowCommand
Session("bu_id") = e.CommandArgument
If e.CommandName = "ShowDetail" Then
Response.Redirect("Show_Detail.aspx")
ElseIf e.CommandName = "Download" Then
Dim strFileName As String = e.CommandArgument.ToString.Split("|")(1).Trim
Dim bu_id As String = e.CommandArgument.ToString.Split("|")(0).Trim
Dim ext As String = strFileName.Trim
Dim saveDir As String = "Files\bug\"
Dim appPath As String = Request.PhysicalApplicationPath
Try
DownloadFile(appPath + saveDir + bu_id + ext, True)
Catch ex As Exception

End Try
ElseIf e.CommandName = "barresi" Then
Response.Redirect("barresi.aspx")
End If
End Sub




Private Sub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
Dim path As Path
Dim fullpath = IO.Path.GetFullPath(fname)
Dim name = IO.Path.GetFileName(fullpath)
Dim ext = IO.Path.GetExtension(fullpath)
Dim type As String = ""
If Not IsDBNull(ext) Then
ext = LCase(ext)
End If
Select Case ext
Case ".jpg", ".jpeg"
type = "text/HTML"
Case ".gif"
type = "text/plain"
Case ".doc", ".rtf"
type = "Application/msword"
Case ".csv", ".xls"
type = "Application/x-msexcel"
Case Else
type = "text/plain"
End Select
If (forceDownload) Then
Response.AppendHeader("content-disposition", "attachment; filename=" + name)
End If
If type <> "" Then
Response.ContentType = type
End If
Response.WriteFile(fullpath)
Response.End()
End Sub

mojniknam
چهارشنبه 27 آذر 1387, 18:05 عصر
یه راه حل خیلی ساده تر اینکه فایل رو زیپ کن و دکمه ای بساز و آدرس اون فایل زیپ رو بهش بده.
ولی اگه نمیخوای فایلت بصورت زیپ باشه باید از راه حل بالا استفاده کنی