Uploading files in ASP.NET MVC using AJAX - ajax

Below image depicts a screen in my sample application which has
file upload functionality.
Below are the steps I followed to upload and save files in Database.
In this screen once user clicks "Browse" button and selects the file(s) that need to uploaded, an AJAX request will be made to server(MVC application) for each file selected.
For each upload AJAX request, server will save the file data(byte array) in session array variable and sends JSON response(which has file name and file size) to Browser.
Browser will display JSON response.
Once user clicks the "Submit" button , the file information which is already there in session variable will be stored in Database.
Following above approach I could implement the desired upload functionality.
Question: I would like to know whether this approach is correct, am I making
any mistake?

Related

Uploading image via Slack interactive button

I have such interactive button I place at the bottom of backend-generated message:
The button click calls the dialog:
If I enter the public URL of an image in the 'URL' field it is successfully sent to the backend and I'm able to update the backend-generated message with an image. But instead of uploading file somewhere and copying it there I'd like to click 'upload image' in the dialog and select file from a disk. Is this possible?
No. Uploading files is currently not supported by Slack Dialogs.
But you could implement it yourself with an upload script that runs in the browser and is called by a link button from Slack. This would work similar to this example for a file download.
The link button is a variation of an message button and has to be placed in a message (e.g. next to your Add note button), but can not be placed inside the Dialog.
Here is the basic outline:
User clicks on "Add image" button in message
Browse opens and runs upload script
The upload script requests the user to specify which file to upload
Script uploads the file (e.g. to your server or Slack) and links it to
the user's request
Things to consider:
You have to link the current session with your script, e.g. by transferring an ID in the link (which might infer security concerns)
This upload function will not be modal like you dialog, so your app needs to be able to handle an asynchronous / parallel upload of files
Check out these pages for more details on uploading files via browser:
What is the best way to upload files in a modern browser
Using files from web applications

Django - download file without reloading the page

The flow looks like that:
user is filling the form
form is passed to the server by ajax
form is saved to db, then the pdf with the form data is created and saved in the app folder (probably a bad move here...)
ajax success causes the page to append a button 'Download' with value equal to current pdf's name so button 'Download' appears to the user
If user presses the button the very pdf that was just saved is gonna download.
Refreshing the page makes the button disappear.
I've got stuck on point 5. I have created another ajax (to avoid reloading the page) bound to the Download button. It correctly asks the server to look for the file, creates a django File object: pdf_file = File(open(file_path, 'rb'))
and creates a HttpResponse with file, and content_type='application/pdf' or 'application/download'.
response['Content-Disposition'] is attachment.
Then the ajax returns response - only it does not. Server raises no error but ajax error function is called.
I've read that downloading with ajax is not possible. Could you help me a bit to get it straight? If above snippets are not clear, I shall provide more code.
Python 3.5, Django 1.10
Do like
window.location.href = "/url/to/downloadfile/"
in javascript after success of posting form. OR
#html
<button onclick="myfiledownload()">Download</download>
#javascript
function myfiledownload(){
window.location.href = "/url/to/downloadfile/"
}
Instead of using ajax to download the file bind the button to a download link where the file may be hosted
https://www.mywebsite/download/?fileid=3247023
You should at least seperate your file in a media root
Note: in production you will have to use a cdn to host your static files

Uploading and saving images in mvc 3 before post

I have a problem with images in my web app. let say I have a form where there are some text fields, dropdowns, upload ajax control for multiple image upload and button Save.
I know how to upload images and posting a form and saving everything to database but the problem is that I want to achieve the following user experience:
User can upload multiple images and browser after successful upload shows thumbnail (this I know how to do it)...the problem here is that I do not want to save images to db before button save is pressed. Is there any convinient how to save temporary images...I have tried temp table which after button is pressed, copies all images to image table but here is browser refresh problem that looses all the data and images stay in a temp table. Can you use a session for storing temporary images or any other way (I think session is not particulary good idea for such things.)
One easy way to achieve that is to use the HTML5 File API and display a thumbnail once the user selects a file to upload. You don't even need to waste bandwidth as everything happens on the client. Once the user submits the form, then the image will be uploaded to the server.
Another possibility is to use one of the gazillions of available AJAX upload controls such as Uploadify, Valums Ajax Upload, Uploadify, the jQuery form plugin, ... which will allow you to upload the file to the server using an AJAX request so that you can resize it there and return the resized image to the client that you could display the thumbnail on the client.

how to implement FILE UPLOAD as in UPDATE PANEL in asp.net mvc3?

i have a mvc application to enter user profile to the system
i need to upload a image of user as well
but problem i face here is when i upload the image (calling functions in upload controller) all the data entered in other textboxes are gone.
what i need is when i click upload i want the image file to be saved(in a temp folder) and the remaining info in the textbox to remain intact. i have separate button to save the info to datbase which will save all data from TB and also the image(after converting it to byte array) to the databse
You can not upload file with pure AJAX. You need to create some iframe solution for that.

Handling an ASP.NET MVC FileResult returned in an (jQuery) Ajax call

Goal:
I want to let my users download a file on my webpage. I want a new window to open and the file to be either displayed or downloaded there.
My implementation:
This file however, first has to be generated on the server-side, which might take a while. When the user clicks the button to download the file, I do an ajax call and show a waiting animation until I get a response. The controller action that handles the call will generate the file (PDF) and return a FileResult. Now in the succes function of my ajax call back in javascript, I get the file data.
Problem: I have no Idea what I'm supposed to do with this data to get it to the user.
Workaround:
Right now I use a workaround where I do not return the file in the ajax call, but store it in session. In the succes function I do window.open("/controller/getPDFFromSession") which will download the file. However, I prefer not to use the session for these kind of things.
Thanks in advance.
Problem: I have no Idea what I'm supposed to do with this data to get it to the user.
You shouldn't use AJAX for downloading files for this reason. You could do the following:
The user clicks on the download button
Using javascript you show some progress image informing him that he will have to wait
Using javascript you generate and inject a hidden iframe into the DOM having its src property pointing to the controller action supposed to generate the file
Once the iframe is loaded you could hide the progress image

Resources