Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. saving a Word Doc from address bar in .net/C#

saving a Word Doc from address bar in .net/C#

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pita32
    wrote on last edited by
    #1

    I've got a situation in my .Net application where I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name. To get the input file from the address bar:

    HttpPostedFileBase fileContent = Request.Files[0];

    Then the save:

    fileContent.SaveAs(fullPath);

    Unfortunately the SaveAs does not work, as when you get the Word Document from the Response object, it blanks it out when doing the SaveAs. So what I need to do is get the Word Document that the user selected, and save the contents of that Word Document to a Word Document with a different, standardized name. so what I have so far is

    using (var documentReader = new StreamReader(fileContent.InputStream))
    {
    var readDoc = documentReader.ReadToEnd();
    }

    I know my readDoc variable contains the selected Word document contents, and it appears to do the ReadToEnd correctly, the problem now is, I need to be able to save those contents stored in readDoc to a different Word Document with a different name. Any ideas on how to go about this?

    L Richard DeemingR 3 Replies Last reply
    0
    • P Pita32

      I've got a situation in my .Net application where I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name. To get the input file from the address bar:

      HttpPostedFileBase fileContent = Request.Files[0];

      Then the save:

      fileContent.SaveAs(fullPath);

      Unfortunately the SaveAs does not work, as when you get the Word Document from the Response object, it blanks it out when doing the SaveAs. So what I need to do is get the Word Document that the user selected, and save the contents of that Word Document to a Word Document with a different, standardized name. so what I have so far is

      using (var documentReader = new StreamReader(fileContent.InputStream))
      {
      var readDoc = documentReader.ReadToEnd();
      }

      I know my readDoc variable contains the selected Word document contents, and it appears to do the ReadToEnd correctly, the problem now is, I need to be able to save those contents stored in readDoc to a different Word Document with a different name. Any ideas on how to go about this?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Once you have the file name, you don't have to "read" the file; you can just copy it with a new name. [File.Copy Method (System.IO) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=netcore-3.1)

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      D 1 Reply Last reply
      0
      • L Lost User

        Once you have the file name, you don't have to "read" the file; you can just copy it with a new name. [File.Copy Method (System.IO) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=netcore-3.1)

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        D Offline
        D Offline
        DerekT P
        wrote on last edited by
        #3

        No, because as I understand it, the file is being uploaded to a webserver. File.Copy works on the local system only; could be a bit of an issue if you could just copy files by name from a remote client!

        L 1 Reply Last reply
        0
        • D DerekT P

          No, because as I understand it, the file is being uploaded to a webserver. File.Copy works on the local system only; could be a bit of an issue if you could just copy files by name from a remote client!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Frankly, I couldn't tell where he was planning to copy from or to, or why the document had to be opened. Congrats on your insight. And no, I wasn't "copying" from "client to server."

          Quote:

          I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name.

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          D 1 Reply Last reply
          0
          • L Lost User

            Frankly, I couldn't tell where he was planning to copy from or to, or why the document had to be opened. Congrats on your insight. And no, I wasn't "copying" from "client to server."

            Quote:

            I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name.

            It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

            D Offline
            D Offline
            DerekT P
            wrote on last edited by
            #5

            True, it's certainly confusing. I'd latched on (probably incorrectly) to his reference to the "response object". But then it's posted in "General" rather than "Web" so you're probably right.

            1 Reply Last reply
            0
            • P Pita32

              I've got a situation in my .Net application where I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name. To get the input file from the address bar:

              HttpPostedFileBase fileContent = Request.Files[0];

              Then the save:

              fileContent.SaveAs(fullPath);

              Unfortunately the SaveAs does not work, as when you get the Word Document from the Response object, it blanks it out when doing the SaveAs. So what I need to do is get the Word Document that the user selected, and save the contents of that Word Document to a Word Document with a different, standardized name. so what I have so far is

              using (var documentReader = new StreamReader(fileContent.InputStream))
              {
              var readDoc = documentReader.ReadToEnd();
              }

              I know my readDoc variable contains the selected Word document contents, and it appears to do the ReadToEnd correctly, the problem now is, I need to be able to save those contents stored in readDoc to a different Word Document with a different name. Any ideas on how to go about this?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Once you have read all the file data you just need to write it out to the place where it is to be saved:

              using (var documentReader = new StreamReader(fileContent.InputStream))
              {
              var readDoc = documentReader.ReadToEnd();

              // write the text to a new file
              StreamWriter writer = new StreamWriter(saveasfilename);
              writer.Write(readDoc);
              writer.Close();
              

              }

              This is an over simplification, and could cause problems for Word document type files, as they are not text streams. A better solution would be to read and write byte arrays so the actual format of the file's content is copied verbatim.

              P 1 Reply Last reply
              0
              • L Lost User

                Once you have read all the file data you just need to write it out to the place where it is to be saved:

                using (var documentReader = new StreamReader(fileContent.InputStream))
                {
                var readDoc = documentReader.ReadToEnd();

                // write the text to a new file
                StreamWriter writer = new StreamWriter(saveasfilename);
                writer.Write(readDoc);
                writer.Close();
                

                }

                This is an over simplification, and could cause problems for Word document type files, as they are not text streams. A better solution would be to read and write byte arrays so the actual format of the file's content is copied verbatim.

                P Offline
                P Offline
                Pita32
                wrote on last edited by
                #7

                Hi Richard - Yes this indeed is a Word document, which is the problem. I get the Word document from the user via a popup modal, via the statement

                HttpPostedFileBase filecontent = Request.Files[0];

                it's when I try to do the save

                filecontent.SaveAs(fullpath);

                where it fails, as it writes out a blank document. Could you give me an idea or example of your read and write bytes proposed solution? I'm not familiar. Thanks, appreciate it.

                L 1 Reply Last reply
                0
                • P Pita32

                  Hi Richard - Yes this indeed is a Word document, which is the problem. I get the Word document from the user via a popup modal, via the statement

                  HttpPostedFileBase filecontent = Request.Files[0];

                  it's when I try to do the save

                  filecontent.SaveAs(fullpath);

                  where it fails, as it writes out a blank document. Could you give me an idea or example of your read and write bytes proposed solution? I'm not familiar. Thanks, appreciate it.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  You are using an abstract class that does not do anything. You need to create your own class that is based on HttpPostedFileBase Class (System.Web) | Microsoft Docs[^] as explained in the documentation, and implement the methods that it contains.

                  Richard DeemingR 1 Reply Last reply
                  0
                  • L Lost User

                    You are using an abstract class that does not do anything. You need to create your own class that is based on HttpPostedFileBase Class (System.Web) | Microsoft Docs[^] as explained in the documentation, and implement the methods that it contains.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    That's not correct. There's already a concrete class in the framework which inherits from HttpPostedFileBase. It's recommended to use the HttpPostedFileBase class in MVC so that it's easier to test.


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    L 1 Reply Last reply
                    0
                    • P Pita32

                      I've got a situation in my .Net application where I have a popup modal where the user will select a Word document and my process will save it to a repository under a different standardized name. To get the input file from the address bar:

                      HttpPostedFileBase fileContent = Request.Files[0];

                      Then the save:

                      fileContent.SaveAs(fullPath);

                      Unfortunately the SaveAs does not work, as when you get the Word Document from the Response object, it blanks it out when doing the SaveAs. So what I need to do is get the Word Document that the user selected, and save the contents of that Word Document to a Word Document with a different, standardized name. so what I have so far is

                      using (var documentReader = new StreamReader(fileContent.InputStream))
                      {
                      var readDoc = documentReader.ReadToEnd();
                      }

                      I know my readDoc variable contains the selected Word document contents, and it appears to do the ReadToEnd correctly, the problem now is, I need to be able to save those contents stored in readDoc to a different Word Document with a different name. Any ideas on how to go about this?

                      Richard DeemingR Offline
                      Richard DeemingR Offline
                      Richard Deeming
                      wrote on last edited by
                      #10

                      fileContent.SaveAs should work fine. Are you reading the posted file multiple times?


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                      1 Reply Last reply
                      0
                      • Richard DeemingR Richard Deeming

                        That's not correct. There's already a concrete class in the framework which inherits from HttpPostedFileBase. It's recommended to use the HttpPostedFileBase class in MVC so that it's easier to test.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        My bad, but I couldn't see that in the documentation anywhere.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups