So I'm using WebClient to upload a file to a server. It works great except for one problem. If the file is opened by another program then it will not upload. For instance, if it's a word document that's been saved but is still opened by word then it fails to upload. Is there a way to force it to read whatever is there and upload it?
If i understand your question right then you can use this solution to test if the file is open and then close it if it is. It's a useful helper class i've used in the past.
https://stackoverflow.com/a/1247326/4612655
Related
I'm posting with tags asp.net and excel because that is the origination of my problem, but I'm not really sure this is the right place - ultimately, my problem is that I have two files (served by an ASP.Net application) which are identical based on a binary file compare using
fc /B A.xls B.xls
However, they exhibit different behavior: the first one opens fine in Excel; the second one does not. I conclude, then, that there is something different about the files beyond what the FC utility checks.
I have tried sending these two files to a friend to ask for his help, but discovered that when I do so, the problem file gets "fixed". In fact, if I do just about anything with this file, it gets "fixed". By fixed, I mean that it then opens fine in Excel. For example, if I zip it, then extract it from the zip, it is fine. If I open in Notepad++ and "Save As", it is fine. Same with Wordpad. Using plain old Notepad does NOT fix it.
So, obviously, there is some difference about these two files that I am missing.
I'm not sure if I will have any luck asking people to visit a random website, but if you want to see an example of the behavior, I have created a minimal page to duplicate the problem at http://rodj.me/ExcelTest
Click on the link for "MinimalHtml.aspx", and the app will serve an HTML based xls file using the following in the Page Load:
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "filename=MinimalHtml.xls");
}
Depending on your browser and browser settings (my tests have been in Chrome), you may get Excel opened with a blank page. Regardless, you should get the file MinimalHtml.xls downloaded. It is a plain text file. You should find that this file will NOT open in Excel. However, if you zip the file, then extract it from zip, it WILL open.
I'm curious about what other file differences I'm missing when just doing an FC compare, but ultimately, I need to get the ASP.Net application corrected to serve the HTML version of the Excel file correctly. Interestingly, if I create an XML version of the spreadsheet, it downloads/opens fine. That is what the "MinimalXml.aspx" link does.
Can anyone help with either 1) how to figure out what is different about the two files; or 2) what must change in the ASP.Net application to get it to serve the file correctly?
I think your problem might be a Microsoft security patch. See this article:
Infoworld article
When you open the file directly, the patch causes the issue which results in a blank page because the file contents is HTML not Excel. When you download the file in a Zip file and unzip it, it is deemed safe and opens correctly.
Here is my code to download file using C#
Client.DownloadFile("link","file");
I want to modify it in such a way so that it will create file, only if download is successful. Currently, if I already have a file in the folder where I am trying to download, the above code deletes current file, if download is not successful.
Any suggestions?
Regards,
I see two ways:
Download the file to a temporary name, and when the download is complete you move it to the right place.
Use the DownloadData method to get the data as a byte array instead, and File.WriteAllBytes to save it to the file when you have all the data.
The first option works better for large files, and the second for small files.
Well recently having a problem with GemBox.SpreadSheet 3.1. Right now the program will write an excel file, but when opening the file it will attempt to open the file saying "Excel found unreadable content" then asks yes or no if you wanna try to recover the document. Click yes, it then says "The file is a macro-free file, but contains macro-enabled content".
However, when the program makes different files without the Macros, it does not say it is corrupt and works just fine.
So my question is, How do you enable the Macro content using GemBox API? Or is there just bad coding somewhere?
EDIT:
I figured out that the program was saving the wrong extension.
I figured out that that in order for Macro-enabled content, the file needs to be stored in XLSM, NOT XLSX!
The prompt that we usually get when downloading a file from a website using the IE is hampering the download of a tar file for me .
I am using C# .
Here is the code snippet .
WebClient wc = new WebClient();
wc.DownloadFileAsync(#"www.somelink.com", #"C:\a.tgz");
What i get is not a valid a.tgz file.
I am assuming its downloading some content related to the save/open dialog given by the internet browser .
How do I by pass it ?
Are you downloading the entire file? It could be the download is being interrupted prematurely.
The save/open dialog is not a problem, as that is IE's method of displaying non-HTML content. However if the website is responding with a webpage then that could be your problem (for instance "You need to be logged in to proceed" or "Click here to download the file").
Try renaming the file to html and see if you can read it then. Alternatively you can load it in notepad to see if it is plaintext if you are afraid of what the contents are for any reason.
EDIT:
You may also not be fully downloading the file, given the description on MSDN of the methodology, an empty file is placed at the target location, the file is downloaded to a temporary location, then finally it is moved to the target, if the download is being interrupted, by say the program exiting, that would explain the problem you are experiencing.
Short version: Try downloading synchronously and see if that works better.
Suppose I have a program running that periodically adds information to a .CSV file. Is there a way to write to the file while it is already open in Excel? Obviously the changes wouldn't be noticed until the file was re-opened in Excel, but as it stands right now, I'm catching IOException and just starting a new .csv file if the current one is already open.
Excel seems to open the file in exclusive mode, so the only way I can think of would be to write your changes to a temporary file, and then use FileSystemWatcher to see when the file is closed, and overwrite the file.
Not a very good idea, as you could lose data. The whole reason excel locks the file is so that you don't accidentally overwrite changes made in excel.
It sounds like the file is locked. I doubt you will be able to write to that file if it is open in another process.
As a former (and sort of current) VB Programmer, I can tell you Jared is correct - there is no way to do this directly. You can try to copy the file first, make your edits, then attempt to save the file back to its original location until the locked file becomes free. You should be able to copy that file, even while locked.
What about using Excel's object model and automating the addition of the data into the open spreadsheet? You'd probably need to prompt the user somehow to let them know what was happening.