asp.net mvc Upload File ajax - asp.net

Hi ive got an mvc form with a fileupload functionality. Ive got an action that accepts an file and extracts thumbnails from it, after which the user can select the images and then proceed to submit the form. How can post the initial file via ajax, bearing in mind, this is not the final submission on the form and I want to retain user input. ie no postback
Thanks

I use the ajaxupload plugin for jQuery. Lots of sample code is provided on the site. From the site:
[The] plugin creates invisible file input on top of the button you provide, so when user clicks on your button the normal file selection window is shown. And after user selects a file, plugin submits form that contains file input to an iframe. So it isn’t true ajax upload, but brings same user experience.

Browsers don't allow the uploading of files via ajax. There are several good workarounds, however.

Related

Adding lightbox (or something similar) to ASP.NET project

I need to implement some, let's call it "dialog" in some old ASP.NET project that I took over. It's huge project so I'm not allowed to implement new things.
I have a form on which user can attach up to 3 file attachments, enter some 10-15 fields, and when required fields are filled, submit button get's enabled.
What need to be done is press submit, wait for any possible response for server, and then display some sort of dialog, alert, lightbox which would display short summary and list of correctly uploaded files.
What would be the best approach to do this in ASP.NET?
Considering jQuery Lightbox is designed for overlaying images rather than HTML I would recommend using FancyBox or FaceBox instead.

ajax image upload and preview in asp.net

i have created a webform.in that there is file upload.
i want to upload the image and show the thumbnail image on fileupload select,and On page submit i want to save the image path to database.
how can u implement it using ajax.
Thanks in advance.
Strictly speaking there is no way to implement an actual ajax file upload as its not supported by browsers. Html 5 has a lot more stuff you can do with file uploads.
You can implement your own hack using iframes, I have done this before. Basically you have an iframe that just has a form with the fileupload control on it and auto submit the form then call out to the parent window to update it when complete.
Alternatively use a pre built control something like the telerik one, you should be able to find a free one. http://www.telerik.com/products/aspnet-ajax/upload.aspx.

ASP.Net Load Data from another page

i am facing one problem..
I have a page which has some templates related to user..
User use this template to send email..
User can have option to save this template and then he can load the saved templates...
What i want is on click on "Load Template" link. a new page appears which will display all the saved templates for logged in user. this page can contain grid. on select i want to close this load template page. and pass the text data back to previous page. which will display this template into text field. so that user can use saved templates.
How to do this using Asp.Net
You can do this using JavaScript, assuming the template selection window is opened with a call to window.open(). When the template is selected you can communicate and invoke methods (such as passing back the selected template ID) with code similar to this:
window.opener.templateSelected(selectedTemplateID);
window.close();
Here is information about window.opener
I believe that this may be what you're looking for. It's pretty straight forward and is in C#. It is done in .Net as opposed to client side JS.

Running code/script as a result of a form submission in ASP.NET

An outside vendor did some html work for us, and I'm filling in the actual functionality. I have an issue that I need help with.
He created a simple html page that is opened as a modal pop-up. It contains a form with a few input fields and a submit button. On submitting, an email should be sent using info from the input fields.
I turned his simple html page into a simple aspx page, added runat=server to the form, and added the c# code inside script tags to create and send the email.
It technically works but has a big issue. After the information is submitted and the email is sent, the page (which is supposed to just be a modal pop-up type thing) gets reloaded, but it is now no longer a pop-up. It's reloaded as a standalone page.
So I'm trying to find out if there is a way to get the form to just execute those few lines of c# code on submission without reloading the form. I'm somewhat aware of cgi scripts, but from what I've read, that can be buggy with IIS and all. Plus I'd like to think I could get these few lines of code to run without creating a separate executable.
Any help is greatly appreciated.
If you don't want the page to reload after submission, you will need to use AJAX. that is alot more complicated. you would still need the submit.aspx, you cannot send email with javascript.
Code a redirect after the form submission, so instead of getting the same form back in the main document/page, you could get something like a blank page saying "Thanks for your submission!" or something of that nature.
Might be more simple to redirect the user to a result page using Respone.Redirect that displays some sort of "Your email has been sent" message, or even just redirect back to the base page.

How not to upload file on submit

I have a form in asp.net with a FileUpload control inside. Next to this control I have "Upload" button which is used to upload a file to a list of files. The problem is that I also have "Submi"t button used for submiting whole form.
Now when somebody selects a file through browse button and presses on "Submit" not "Upload" file is being uploaded and I wouldn't want that.
I'm hoping for asp.net or general HTML answer
Sounds like the FileUpload control and Upload button should be inside it's own form. With ASP.NET and the 1 server-side form restriction, you may have to resort to regular HTML controls to do this.
Alternatively, you may be able to use JavaScript to clear the FileUpload if Upload isn't clicked. Browsers can be picky about access to file inputs, though (for security reasons) so it may not be accessible.
For the general HTML answer, have your file upload button be part of a separate form from the other form, meaning they are encapsulated within separate Form tags.
For ASP.NET, you're going to have more pain, since ASP.NET by default has only one form per page.
Ultimately, however, I think you're better off rethinking your design. Try to put that file upload on a separate page; if you have a page that does more than one thing, you're inviting confusion on the part of the user.
Submit button will send all of the form's information.
Try putting the FileUpload control outside, on a diferent form.
Your FileUpload control needs to be in its own form if you want to to be submitted separately. When you click the Upload button, the other parts of your form are being submitted as well. That's just how it works.
I would separate the two forms. Using JavaScript or something like that could get to be a pain. The problem with separating the forms is you have to do it in a way where you don't lose your original form, meaning if you have other form information around the file upload you might have a bit of a nightmare on your hands but if you can put the file upload either above or below the form that submit controls then it's easy.
<form id="A">
<button name="submit>
</form>
<form id="B">
<fileupload>
</form>
I use this method on one of my sites and it works quite well.

Resources