File Replace Confirmation - c#

I am developing an ASP.NET 3.5 web application which allows users to upload files to the server. If the user is uploading a file which is already there in the folder then I want to show a ASP.NET AJAX modal popup asking the user whether he wants to replace the existing file or not and continue the operation depending on the user's input. Is there a way to do this?

AFAIK, there is no out-of-the-box functionality to do this. You will have to check for the file existence in code and then display the question.

You don't mention whether you're uploading the file by AJAX or the good'ol fashion way. Since I doubt you'd be asking this question if you were uploading through AJAX (seeing it would be trivial to do this), I have to assume that you're not uploading through AJAX, but that you'd like an AJAX window anyway.
The only way I can think of to do that, is by checking for the file's existence through an AJAX call before you start uploading. This is because when you upload the file, there is no way for the browser to send information back until the upload is complete, and even then you can't use AJAX because the call has to be initiated as an AJAX call as well.
So:
User enters file.
BEFORE sending the file, an AJAX call is made to check whether the file exists
If file exists, ask user, submit form if user wants to replace the file and cancel otherwise.
If file does not exist, submit form.
Hope this helps!

You will need to try the upload using an AJAX call, just sending the filename - the server side method could check for file existence and respond with an error if one exists.
If no error is returned, you can proceed with the upload without the question.
If an error was returned (indicating the file exists), ask the question and follow the users response.

Related

AjaxFileupload multiple asynchronous file upload

I need to find a way to upload asynchronously multiple files.
I want to make a smtp client, User can chose files, create message and then send the mail.
I use ajaxfileupload but it create a folder witch look like c:/windows/tem/_ajaxFileUpload/[a guid]/[uploaded files]
is it possible to determine the guid and then find files to attach without saving it to an other folder.
this must be done after user click on send button.
One possible option is the HTML 5 File Api which allows client side uploads. Here is a tutorial on it http://www.html5rocks.com/en/tutorials/file/dndfiles/
I have used it in conjunction with a MVC 4 C# application, and have successfully got it to post (ajax post) file contents to an API Controller which can then be used to persist the file. While you don't mention if you are using MVC, the same should be possible using any sort of service.
Hope this helps.

Add xml to http redirect

I have a situation whereby I need to redirect a user from a page on my server to a page on another server. In this redirect I need to also send some xml to the server I am redirecting to. This xml file could be quite long so just sending it in the querystring isn't an option.
I have tried attaching the xml in a header but the header doesn't seem to make it to the other end.
I know how to programmatically create requests to send xml and how to redirect, just not sure how to do both at the same time.
In short, I need the xml to piggy-back on the redirect. This redirect will be done from within an MVC Action.
Thanks in advance.
Edit
I have come up with the following potential solution to my problem. Unfortunately it does utilise two requests which I had hoped to avoid.
Basically I send the xml file as a header of a post request which also contains a session id. This is sent asynchronously.
I then redirect the user, passing the same sessionid in the querystring. This acts as a token to link one request to the other. I just need to wait now and find out if the other party are willing to work using two requests.
I also looked in to using an additional page that I could redirect the user to which could contain Javascript to perform an additional form post to the other server with a form that would contain just a single field containing the xml fragment, but this was excluded as a possibility by my boss (rightly so as it seems like a bit of a hack).
Are there any obvious or non-obvious drawbacks to my proposed method, other than the obvious possibility of a race condition between the 2 requests?
You could redirect to http://otherServer/handlerPage.aspx?xmlSource=http%3A%2F%2FfirstServer%2FxmlSource.aspx%3FparameterForXml1%3Dfoo%26parameterForXml2%3Dbar
Then at http://otherServer/handlerPage.aspx the xmlSource parameter is http://firstServer/xmlSource.aspx?parameterForXml1=foo&parameterForXml2=bar which can be used to obtain the XML.

.net webhandler force download dialog to client c#

I am having a bit of a problem with a web applcation I am developing in .net with C#. I am using a WebHandler that responds to an ajax call from the client, and executes a function that creates a file. What I want is to display download dialog at the end of the file creation process, however the Response class does not exist in the context of the function called by the handler. How to force the application to show the download dialog to the user after file creation? I was thinking about uilding a callback function to the ajax call, and when the execution of the first ajax call returns do another ajax call to a different WebHandler that will force the download dialog. Is this the most efficient way to do this? Is this even correct? What other more orthodox ways can I use to achive what I want? Thank you for yor help.
Consider taking AJAX about of the solution and just redirecting or setting the window.location to the WebHandler url, which will trigger the file download dialog on the browser. Once the user accepts the download, the browser will remain on the page.
Maybe there are more details as to why Ajax is needed. If so, please add them to the question.
On the success operation of your ajax call once the file has been created by the handler just navigate to the file... eihter using C# or JavaScript

Allowing the user to save the file from JavaScript

There are a lot of JavaScript utilities now to allow posting a file in an AJAXified way to the server these days. Is there any utility that allows streaming bytes to the client and download a file? Or dues that still have to be a server-side solution? I'm using .NET.
Thanks.
There's no cross-browser support for accessing the filesystem of the client. You could probably do it with Flash/Java, but a much cleaner solution would be to do it in the server and create a download link for the user.
Brian: What you said about Javascript posting files to a server seems an incomplete statement. Only way javascript can post files to a server is by having the user manually selecting the file he/she wants to upload...
To answer your question...
You should be able to issue some sort of ajax call (to a web service, for example), have the web service read the file into a byte array and return it to the client. On the client side, you would need to assemble the byte array. I would assume you'll have to also set the appropriate response type from the web service call.
This seems to be something like what you are looking for.

Send HTTP Post with default browser with C#

I am wondering if it is possible to send POST data with the default browser of a computer in C#.
Here is the situation. My client would like the ability to have their C# application open their browser and send client information to a webform. This webform would be behind a login screen. The assumption from the application side is that once the client data is sent to the login screen, the login screen would pass that information onto the webform to prepopulate it. This would be done over HTTPS and the client would like this to be done with a POST and not a GET as client information would be sent as plain text.
I have found some wonderful solutions that do POSTS and handle the requests. As an example
http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx
So the TL;DR version of this would be
1) Open Browser
2) Open some URL with POST data
Thanks for your help,
Paul
I've handled a similar situation once by generating an HTML page on the fly with a form setup with hidden values for everything. There was a bit of Javascript on the page so that when it loaded, it would submit the form, therefore posting the data as necessary.
I suspect this method would work for you.
Generate a dictionary of fields and values
Generate an HTML page with the Javascript to automatically submit when page is loaded
Write page to a temp location on disk
Launch default browser with that page
Remember though that POST data is sent plaintext as well. POST is generally the way to go for more than a couple fields, as you can fit in more data (2048 byte limit on URLs) and that your user has a friendly URL to see in their browser.
Nothing is sent as plain text when you use SSL, it is encrypted. Unless you set what the default browser is (IE, Firefox, Chrome, etc), then you'll have to figure out what the default browser is and use its API to do this work (if it's possible).
What would probably be must faster and more efficient would be to open the default browser by invoking a URL with Start Process and pass the information on the query string (this is doing a GET instead of a POST, which I know isn't what you're asking for).
The response from the server could be a redirect, and the redirect could send down the filled-out form (storing the values in session or something similar).
That way the complexity is pushed to the website and not the windows application, which should be easier to update if something goes wrong.
HTH
Can you compile your logic in C# and then call it from PowerShell? From PowerShell you can very easily automate Internet Explorer. This is IE only but you might be able to also use WaitnN.
Anything you put at the end of the URL counts as the querystring, which is what GET fills. It is more visible than the POSTed data in the body, but no more secure with regard to a sniffer.
So, in short, no.

Categories