saving a Word Doc from address bar in .net/C#
-
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?
-
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?
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
-
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
-
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!
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
-
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
-
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?
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.
-
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.
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.
-
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.
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.
-
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.
That's not correct. There's already a concrete class in the framework which inherits from
HttpPostedFileBase
. It's recommended to use theHttpPostedFileBase
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
-
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?
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
-
That's not correct. There's already a concrete class in the framework which inherits from
HttpPostedFileBase
. It's recommended to use theHttpPostedFileBase
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