locked
Hyper Link for File Upload ASP.Net RRS feed

  • Question

  • User-1891900014 posted

    After uploading the document, it should show a URL link below the upload control, so user can click and open to see the document. how do i do it?

    Monday, May 21, 2018 8:22 AM

All replies

  • User753101303 posted

    Hi,

    This is just an a link (or a https://msdn.microsoft.com/en-us/library/seey0yt3.aspx control) to the document source. It could point to the actual document or to a location that will serve the document from its real source (if stored outside of the site or in a db). See perhaps https://www.c-sharpcorner.com/UploadFile/b926a6/image-store-and-retrieve-from-database-using-generic-handler/

    and http://www.dailycoding.com/posts/how_to_force_a_file_to_download_in_aspnet.aspx (shown as part of a page but works as well from an ashx handler which you may want to prefer as it is another concern and avoid to go though the whole ASPX page lifecycle to just write directly to the browser output).

    If you need further help please be explicit about the very first problem you are running into when trying to do that...

    Monday, May 21, 2018 8:57 AM
  • User-1716253493 posted

    Upload the document, save it to database, show the data as grid or repeater with hyperlink

    Tuesday, May 22, 2018 12:12 AM
  • User283571144 posted

    Hi JayRayJay,

    According to your description, I suggest you could try to use session to achieve your requirement.

    We could add the upload file's file name in the list and set the list to session.

    Then we could add HyperLink control with the file name path to the page.

    More details, you could refer to below codes:

    ASPX:

        <form id="form1" runat="server">
            <div>
                <asp:FileUpload ID="FileUploadControl" runat="server" />
                <asp:Button runat="server" ID="UploadButton" Text="Upload" OnClick="UploadButton_Click" />
                <br />
                <br />
                <asp:Label runat="server" ID="StatusLabel" Text="Upload status: " />
                <br />
                <asp:Panel ID="Panel1" runat="server"></asp:Panel>
            </div>
        </form>

    Code-behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["filelist"] != null)
                {
                    ShowHyperLink();
                }
                else
                {
                    Session["filelist"] = new List<string>();
                }
             
            }
    
            private void ShowHyperLink()
            {
                Panel1.Controls.Clear(); //to remove all controls
                foreach (var item in (List<string>)Session["filelist"])
                {
                    HyperLink h1 = new HyperLink();
                    h1.Text = item;
                    h1.NavigateUrl = @"http://localhost:52188/uploadfile/" + item;
                    Panel1.Controls.Add(h1);
                }
            }
    
            protected void UploadButton_Click(object sender, EventArgs e)
            {
                if (FileUploadControl.HasFile)
                {
                    try
                    {
                        string filename = Path.GetFileName(FileUploadControl.FileName);
                        List<string> filepaht = (List<string>)Session["filelist"];
                        filepaht.Add(filename);
                        FileUploadControl.SaveAs(Server.MapPath("~/uploadfile/") + filename);
                        StatusLabel.Text = "Upload status: File uploaded!";
                        Session["filelist"] = filepaht;
                        ShowHyperLink();
                    }
                    catch (Exception ex)
                    {
                        StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    }
                }
            }

    Result:

    Best Regards,

    Brando

    Tuesday, May 22, 2018 7:09 AM