How to upload multiple documents and view it in viewer from Gridview using ASP.NET in C#

Gani_tpt 1,546 Reputation points
2024-04-13T11:59:46.5366667+00:00

I am developing ASP.NET application which is end user want to upload multiple documents from the page.

My problem is, i am already developing software like uploading single document.

But, uploading multiple document haven't done so far.

Also, after uploading multiple documents, end user should have the rights to modify or delete the existing documents.

Finally they want to view all the end user documents in one page using gridview or some other paging controls.

when end user clicks the document should open in the document viewer without showing the URL path.

Because, the URL path is the file server path and not in application server path.

below is my page and controls.

page1.aspx


Associate No : dropdown

Section : dropdown

doc1 :: Browse document

doc2 : browse document

page2.aspx


gridview or someother paging control

Associate No Section doc1 doc2


Storing Dcouments file path of ever associate is : \healthnet.mazok.intranet\ProjectM\AssociateDocs\

How to achieve the above scenario in asp.net...?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,266 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,272 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,636 Reputation points Microsoft Vendor
    2024-04-15T08:46:13.8133333+00:00

    Hi @Gani_tpt,

    My problem is, i am already developing software like uploading single document.

    You can simply merge two uploaded single documents. The following code implements doc1, and doc2 is implemented in the same way.

    If you need to display the information in page2.aspx, you only need to save the information in page1.aspx, and then page2 gets the information from the database.

    To view Word documents on ASP.NET pages, you need to use the Microsoft.Office.Interop.Word dll.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript">
            function clear() {
                //Reference the FileUpload and get its Id and Name.
                var fileUpload = $("[id*=FileUpload1]");
                var id = fileUpload.attr("id");
                var name = fileUpload.attr("name");
                //Create a new FileUpload element.
                var newFileUpload = $("<input type = 'file' />");
                //Append it next to the original FileUpload.
                fileUpload.after(newFileUpload);
                //Remove the original FileUpload.
                fileUpload.remove();
                //Set the Id and Name to the new FileUpload.
                newFileUpload.attr("id", id);
                newFileUpload.attr("name", name);
                return false;
            };
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button Text="Upload" runat="server" OnClick="Upload" />
            <asp:Button ID="btnClear" Text="Clear" runat="server" OnClick="clear" OnClientClick="clear()" />
            <div id="dvWord" runat="server"></div>
        </form>
    </body>
    </html>
    
     protected void Upload(object sender, EventArgs e)
     {
         object documentFormat = 8;
         string randomName = DateTime.Now.Ticks.ToString();
         object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
         //string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
         object fileSavePath = Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
         //If Directory not present, create it.
         if (!Directory.Exists(Server.MapPath("~/Temp/")))
         {
             Directory.CreateDirectory(Server.MapPath("~/Temp/"));
         }
         //Upload the word document and save to Temp folder.
         FileUpload1.PostedFile.SaveAs(fileSavePath.ToString());
         //Open the word document in background.
         _Application applicationclass = new Microsoft.Office.Interop.Word.Application();
         applicationclass.Documents.Open(ref fileSavePath);
         applicationclass.Visible = false;
         Document document = applicationclass.ActiveDocument;
         //Save the word document as HTML file.
         document.SaveAs(ref htmlFilePath, ref documentFormat);
         //Close the word document.
         document.Close();
         //Read the saved Html File.
         string wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());
         //Loop and replace the Image Path.
         foreach (Match match in Regex.Matches(wordHTML, "<v:imagedata.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase))
         {
             wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);
         }
         //Delete the Uploaded Word File.
         File.Delete(fileSavePath.ToString());
         File.Delete(htmlFilePath.ToString());
         dvWord.InnerHtml = wordHTML;
     }
     protected void clear(object sender, EventArgs e)
     {
         dvWord.InnerHtml = "";
     }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful