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.
Related
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
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.
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
this works on dev as usual when I put it on UAT the code does something different. A PDF is saved on the server, the class then opens it up using the url of that file.
The URL works fine if I paste it into a browser, but doesn't work from the code.
Here's my code:
Process.Start(openPath);
openPath will look like: "http://www.cbm360.net/test/temp/CBM360Report_1093750.pdf"
The file is there on the server, but it just won't open in the code.
The code is inside a web method called using AJAX, if that makes any difference, I'm not sure.
Does anyone have any suggestions as to why this isn't working?
The Exception is:
System.ComponentModel.Win32Exception: The system cannot find the file specified
Thanks!
Instead of pasting the URL into a browser (I assume this is on the server), try it directly from the command line on the server. Does it work now? Process.Start is not the same as navigating to a URL in the browser, it is more akin to running the URL from the command line. I am not sure what you are trying to achieve. Normally we would use a web request to get a pdf.
I have a SWFUpload on my page, I click the button and the file selct window comes up, I select a file but I do not know how to get the filename that was uploaded? and I dont even know if the file is being uploaded.
Can anyone help me please?
I am using ASP.NET VS2010
I am currently working on swfupload but in java. I may be able to answer your question.
So after you select a file, the flash upload will prepare your files for upload. It will send the request to the upload_url. This call to upload_url is like sending a request with a
< input type="file" name="Filedata"/ > so you can access your file if you get the Filedata after the multipart post. In other words, its just like an html page uploading your files.
Let me know if you need more help.