简体   繁体   中英

How to Create Hyperlink to Uploaded files to open the file

Hi Below is the code for Uploading Multiple files I cant display all the file names at a time it shows only one file name and how to create hyperlink to Uploaded documents to view the files.

 Protected Sub Button1_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs)
    If FileUpload1.HasFile Then
        Try
            FileUpload1.SaveAs("Destinationpath\testing\\" & _
               FileUpload1.FileName)
            Label1.Text = "File name: " & _
          FileUpload1.FileName & "<br>"
    ListBox1.Items.Add(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName))
      Catch ex As Exception
            Label1.Text = "ERROR: " & ex.Message.ToString()
        End Try
    Else
        Label1.Text = "You have not specified a file."
    End If
End Sub
 Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Files.RemoveAt(ListBox1.SelectedIndex)
    ListBox1.Items.Remove(ListBox1.SelectedItem.Text)
    Label1.Text = "File removed"
End Sub

and below is the aspx code

    <div>
    <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true"/><br />
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
     Text="Upload Document" /><br />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label></div>
    <asp:listbox ID="ListBox1" runat="server" Width="175px"></asp:listbox>
    <asp:Button ID="Button2" runat="server" Text="Remove" Width="98px" OnClick="Button2_Click" />

can anyone help me to do this. Thanks..

Use ListBox for this. It will be something like:

protected void UploadButton_Click(object sender, EventArgs e){
    foreach (HttpPostedFile fl in fu.PostedFiles)
    {
        fl.SaveAs(DestinationPath + fl.FileName);
        ListItem li = new ListItem();
        li.Text = fl.FileName;
        ListBox1.Items.Add(li);
    }
}

In your design page:

<asp:Panel ID="pnlFiles" runat="server" />

Your code behind:

Protected Sub Button1_Click(ByVal sender As Object, _ByVal e As System.EventArgs)
    For Each fl as HttpPostedFile in fu.PostedFiles
        Dim fileLink as String = DestinationPath + fl.FileName
        fl.SaveAs(fileLink)
        Dim hpr as New HyperLink
        hpr.Text = "Download file"
        hpr.NavigateUrl = fileLink
        pnlFiles.Controls.Add(hpr)
    Next
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM