ASP.NET: Modify and download docx file - c#

I have a docx file which I would like to modify according to web user input. So, after the user submits the form on the web page, I need to modify the original docx file, and then download it to the user.
I try to store the original file as a resource file in my project, but I can't open it programatically.
That's what I tried in the post controller:
using Microsoft.Office.Interop.Word;
...
Application app = new Application();
Document document = app.Documents.Open(Properties.Resources.___, ReadOnly: false);
But I received a System.Runtime.InteropServices.COMException (0x80020005 (DISP_E_TYPEMISMATCH))
I also don't know how to download the modified file. In a simple console application document.SaveAs2(newPath); worked, but it doesn't seem to work for downloading.
(I'm not even sure that this whole way could work. If I can't use Microsoft.Office.Interop.Word this way, please let me know.)

If you want to manipulate docx files it's best to use the OpenXML API rather than the InterOP. See https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk?redirectedfrom=MSDN

Related

Upload video to SharePoint library by code

I am making a app for SharePoint and have a form with several inputs for text and for files as well. The input for files works fine for regular files such as Word, Excel, PowerPoint, images etc... but wont work with videos. Is it possible to upload a video to a document library by code? Or cant you do that simply because the files is usually to big and would take long to upload?
The files is of the type HttpPostedFileBase and sent by a normal mvc form.
And this is the code for uploading it to the folder
FileCreationInformation attachFileInfo = new FileCreationInformation();
attachFileInfo.ContentStream = pNewRequest.FileToUpload1.InputStream;
attachFileInfo.Url = Path.GetFileName(pNewRequest.FileToUpload1.FileName);
newFolder.Files.Add(attachFileInfo);
According to this StackExchange answer, SP videos are organized in special VideoSet folders which seem to be similar to doc sets.
You said you're using MVC to submit the files so I'm assuming you're using CSOM on an external server. Here's an example for handling video uploads via CSOM.

Interact with aspx page to download a file from C#

I'm attempting to automate a task I have of interacting with a website form to download a file.
In human terms, I select a label from a list of options, and then press a submit button. The website then automatically downloads a zip file to the downloads folder.
The actions described above have the net effect of running this code in a browser console:
$('#ListBox1').val($('#ListBox1 option:first')) && $('#Download_0').click()
My question is, how can I download this zip file by running the code from a C# program? Ideally, I would also be able to specify where the file downloads to, although I'm not sure if this is possible.
Please note, this question is not asking how to download a file using C# (see: How to download a file from a website in C# and .net - Downloading a file from a website using C#). No such file exists on the website for me to download. The resulting file is generated and downloaded automatically by the two actions above.
EDIT:
The following code does download a file, however, the file is 24kb (the zip file I'm expecting is around 8mb) and unopenable:
using (WebClient client = new WebClient())
{
client.DownloadFile("https://cdr.ffiec.gov/public/PWS/DownloadBulkData.aspx", AppDomain.CurrentDomain.BaseDirectory + "download.zip");
}
EDIT2:
What I'm attempting to do is download the most recent zip file available containing bulk call report data. The website selects the most recently available data automatically, but I have to manually select the type of file I want to download. As such, the web request won't be static and can't be hard-coded into the program, which is why I'm attempting to interact with the webpage.

Open a document(.doc,.pdf) using Google doc viewer - C#

I'm trying to open a document on server through google docs.
I can get the path and name of the file on the server.
And then on Button click event I wrote a method to open the file through google document reader.
string path = \\xxx-yyyyy-zzz\DocShare\sample1.doc //path of the file on server
Response.Redirect("http://docs.google.com/viewer?url=" + path);
When I run this code, I'm getting something like below
Can some one help me to find where did I go wrong? Is there any other method to open any document(ppt, doc, pdf etc) using google document reader.
TIA
What is \\xxx-yyyyy-zzz\DocShare\sample1.doc and where it is ?
A relative url dosen't specify a protocol & domain which makes the
browser to assume the document is referd from the same site/domain.
Please verify if you can access the document itself with the relative path.
http://docs.google.com/viewer?url=https://s3.amazonaws.com/scotchio/First-Node-App.pdf works for me as i can access the pdf. Try using the absolute url of the document
It doesn't work most likely because the file is not accessible from the Internet. The google doc previewer needs to be able to access the file in order to display it.

WebClient.DownloadFile File Corrupt

I'm trying to download files directly from a list of urls.
I was able to download most files successfully except for the .docx. I was able to download the .docx file, but when I try to open it, the error message shows that "The file is corrupt and cannot be opened", when I try to repair it with Microsoft Word, I got another error message saying "Microsoft Office cannot open this file because some parts are missing or invalid". I don't have any issue when download pdf files.
My code is very simple and it looks like this:
WebClient webClient = new WebClient();
webClient.DownloadFile("http://somehost/somefile.docx", "C:\\somefolder\\somefile.docx");
webClient.Dispose(); //I added this line just to see if it will fix the problem but no it didn't
I went to the urls in the browser and make sure that the files does exist and are not corrupted. The urls are just fine and I was able to download files directly from the urls in a browser and the file opens.
Additional Information:
I did find one thing that's different for pdf url and docx url, but I really don't think it has anything to do with my problem. When I go to the pdf url in a browser, the pdf was displayed in the browser. However, when I go to the docx url, the page doesn't show anything, but the download for the file automatically starts. I don't think this will make a difference but just FYI.
EDIT 10:38AM
I just tried the Async method. I was able to download the docx file and open it, but it appear as a blank word document, which is still not correct. The same docx file I download from the browser does have content.
webClient.DownloadFileAsync(new Uri("http://somehost/somefile.docx"),"C:\\somefolder\\somefile.docx");
DownloadFileAsync downloads file in background, and your application probably terminates before download is completed.
You should wait for DownloadFileCompleted event, or use a DownloadFile method that will wait until file will be downloaded.
Thank you everyone for trying to help, I really appreciate it.
I realize that the problem was actually me not concatenating the url correctly. Right, a stupid mistake I made...
WebClient didn't throw error for incorrect format (for whatever reason), and my log file was not logging the actual url that I was trying to connect to, so I didn't realize it was doing the wrong thing.
Anyway, thank you all for the help and the comments that help me figure out what the problem was.

asp.net check if a file from the Response.OutputStream has been saved/opened

I am building an excel file (using EPPLUS) on a web page which gives the option to either save or open the file.
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=Excel_List.xlsx");
package.SaveAs(Response.OutputStream);
Response.End();
That works fine. I just want to know how can I check if the user has either opened/saved the file?
Is there an event for that?
The simplest answer is you cannot. Once the server has handed the file off to the client ASP.NET has no control over it.
You could, in theory, place a macro in the Excel file to call a URL on your server, but that's assuming the user opens the Excel file and allows for the execution of the macro (which is disabled in modern Excel versions).
You cannot determine it. After the output is generated you have no info about download process or actions that user takes on downloaded file. It is caused mostly by web browser security limitations.
No, that is not what HTTP is for. If you can successfully send all data from the server, you may assume the client has received it all. Client may then discard, save, print, copy, do whatever they like with the data.
If you want to perform such a check, you should do it from the Excel file itself. But then again you shouldn't, because of privacy. If I ever caught an Excel file phoning home, I don't know what'll happen.
It isn't possible to know if they actually opened it in Excel without adding a macro to the file, but there are ways that you can send a confirmation to the server that the download was completed successfully. For this you would need to embed a rich client in the browser to handle the HTTP request rather than doing it with the browser alone. Something like SoftArtisans XFile can be used for this. Our .NET Excel library also comes with an ActiveX control that can manage the download process (called the OfficeWriter Assistant).
Disclaimer: I work for SoftArtisans

Categories

Resources