I have a requirement to download a file from a url, then POST it to another site via single js call.
Use case:
User enter a file URL then click Re-Upload button
Conditions:
The file will be either audio/wav, audio/mp3, or a zip file with multiple wav and mp3
I need to POST using multipart/form-data
Questions:
How do I create multipart/form-data request via js manually?
Correct me if I am wrong, but I think I need to store the file content into an input field once I download it.
How to encode this file content into a string that will comes up as byte[] when uploaded?
Related
I have some files in the local and know the path of those files.
I'm trying to upload the file using AJAX.
How to upload the file using AJAX without using FORM.
enter code here
NOTE: I've the FileReader object of the file.
Please share if u have any idea.
Thanks
You cannot upload a file from the local computer unless triggered by the user (the user should select the file), you can upload a file with jQuery:
https://github.com/blueimp/jQuery-File-Upload
https://github.com/hayageek/jquery-upload-file
I am using REST client to upload a profile photo to IBM connections but i am struck at the part where you have to send binary image file along with the PUT request. So far I've done this:-
request.put("https://servername.com"/profiles/photo.do?
key="profileuserID"),
ContentType:image/jpeg
i have tried to put insert image by encoding it into base64 format or adding into file field in REST client. But it always returns nothing. It does remove the previous profile picture but it doesnt upload the new one.
Now i want to ask how to include binary image file in the request payload?
I need to download file from javascript. I send json with $.post, Rails server generates xls file and sends it back with send_data. Response has nex headers:
Content-Disposition:attachment; filename="preflist.xls"
Content-Transfer-Encoding:binary
Content-Type:application/ms-excel
How can I download this file after post request?
As I understand it you need to trigger normal browser file download after posting data to the server.
There are two ways of doing this:
After success of post you redirect your browser to an URL where the posted file is located
If the only source of the file is on the response of the post you will have to use the FileReader API, though that is not supported by all browsers. You combine reading the file from the result of the post with readAsDataURL(). The result of this is something you can combine with a hyperlink and the download attribute:
Then use javascript or jQuery to click that hyperlink.
http://blog.teamtreehouse.com/reading-files-using-the-html5-filereader-api
Hope this helps!
I'm trying to use the JSSoundRecorder(https://github.com/daaain/JSSoundRecorder) for recording a file on my website. I would like to send the resulting wav along with a form where I add more info. Is there a way to save the Blob in my form? So far I can only find a way to directly send the blob to the server using a Ajax request. Is there a way to put the result of the recorder in a hidden file or another smart solution?
You won't be able to upload it as a file [1] (via <input type="file">) with a form, but you could try to base64 encode it and submit it via <input type="text">. You'd then have to decode it on the server before writing it to a file. This JSPerf has a few different methods of converting a Blob to base64: http://jsperf.com/blob-base64-conversion
[1] How to set a value to a file input in HTML?
I'm doing a small HTML project of converting a doc file into another file (which is based on user preference). The website will have the user pickup the file, and choose their preferences on how the file should be processed, before sending it to a Java servlet. After processing the file, the servlet will then send the processed file as a downloadable file, which then will be automatically downloaded to the user's browser download folder.
At first, i used the HTML's form tag, with various input on it. After submitting the form, the processed file was auto-downloaded to my browser's download folder.
But then i changed the pickup file method into drag and drop, and used XMLHttpRequest to send the formdata, along with the dropped file to the servlet. The formdata was successfully sent to the servlet, and the servlet processed the file normally. But after that, the servlet didn't send the processed file to my browser as a downloadable file. I checked on the inspector, on the response under network tab, and it showed the processed file content, yet i didn't get the processed file like i would normally get using form tag method.
So i was wondering did i do something wrong in my XHR code below? I just want to get the processed file from the servlet as downloadable file, and not showing it on my page. The HTML form tag works fine and the servlet returned the processed file as auto-download file, but with XHR, the processed file was only shown in the response tab under network in the inspector, no downloadable file or whatsoever.
function formHandler(form) {
var formdata = new FormData(form);
formdata.append("inputFile", doc);
var xhr = new XMLHttpRequest();
xhr.open('POST', "excelServlet", true);
xhr.send(formdata);
}
Ok i get the answer for this already.
AJAX cannot download the response from server directly like form's submit() method. =.=''' The response will be shown in the inspector's response, but user won't be able to download it.