download files using ftp asp.net

2019-08-31 07:58发布

问题:

I'm working on building a online directory of exam papers for my college. Students get to select the year, then the class for which they want to see the paper, this is all done. I don't know how to get them to download the papers. After you select the class all the relevant papers get displayed in form of hyperlinks which when clicked allow you to view or download the paper how to go about this? I was thinking of using ftp server(drivehq to be specific)and asp.net website but I ain't finding good tutorials. All the tutorials are showing how to download a single specific file not multiple. Do I have to code for every single file present on my server? Is there a better way to do this?

回答1:

First you have to decide where you are going to save the files. It could be either in a folder on the application root, or if you are going to use SQL FileStreaming(this is very handy as it store the filestream on the table)

I will post a sample code structure to give you a head start, but you will have to figure out how to best fit this to suit your need. I have commented on methods you will have to write, so consider calling web-service methods in these cases in the long run.

I am assuming you have a class alreday created for ExamPaper wich maps to the file you are downloading.

I am getting a list of ExamPapers and binding the list to a datalist which has linkbutton template. And I pass the FileID as a command argument on link button click. Then I store FileID on a session variable. In the newPage, on page load, I simply take that FileID and get relevant data and the stream and append it to response. I am assuming you download word documents, if not you will have to change the Content Type.

Give this a try and if you get any question, try searching on google or ask in this forum.

code

ListPage.aspx

<asp:DataList ID="PaperList" runat="server" RepeatDirection="Vertical" RepeatLayout="Table">                   
    <ItemTemplate>
    <table>
        <tr>
            <td><asp:LinkButton ID="LinkButton1" CommandArgument ='<%# Eval("FileID")%>' runat="server" Text='<%# Eval("FileName")%>' OnCommand ="btnDownload_Command"></asp:LinkButton></td>
        </tr>
    </table>
    </ItemTemplate>
</asp:DataList>

ListPage.aspx.vb

Dim PaperList As New List(Of ExamPaper)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  'You have to write a method to return the list of file with fileName and the unique id(PK)

  PaperList = GetListofPapers()

  'databind the list
  Me.PaperList.DataSource = DTItems
  Me.PaperList.DataBind()                             

End Sub

Protected Sub btnDownload_Command(sender As Object, e As CommandEventArgs)
  Session.Add("FileID", e.CommandArgument)
  Response.Redirect("DownloadDoc.aspx?, False)
End Sub

DownloadDoc.aspx.cs

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Dim fileID = Session.Item("FileID")
  Dim fileInfo As New ExamPaper
  Dim binarystream() As Byte

  ' Methot to get other file Info
  fileInfor = getPaperInfor(FileID) 

  ' write the method to convert file to binary
  binarystream = GetStream(fileID) 

  Response.ClearHeaders()
  Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileInfor.FileName )
  Response.ContentType = "application/vnd.openxmlformats-  officedocument.wordprocessingml.document"
  If Not binarystream Is Nothing Then
            Response.BinaryWrite(binarystream)
            Response.Flush()
            Response.Close()
  End If
End Sub