JavaScript: Upload file

Pure JS You can use fetch optionally with await-try-catch let photo = document.getElementById(“image-file”).files[0]; let formData = new FormData(); formData.append(“photo”, photo); fetch(‘/upload/image’, {method: “POST”, body: formData}); async function SavePhoto(inp) { let user = { name:’john’, age:34 }; let formData = new FormData(); let photo = inp.files[0]; formData.append(“photo”, photo); formData.append(“user”, JSON.stringify(user)); const ctrl = new AbortController() // … Read more

How do I upload a file with metadata using a REST web service?

I agree with Greg that a two phase approach is a reasonable solution, however I would do it the other way around. I would do: POST http://server/data/media body: { “Name”: “Test”, “Latitude”: 12.59817, “Longitude”: 52.12873 } To create the metadata entry and return a response like: 201 Created Location: http://server/data/media/21323 { “Name”: “Test”, “Latitude”: 12.59817, … Read more

REST API – file (ie images) processing – best practices

OP here (I am answering this question after two years, the post made by Daniel Cerecedo was not bad at a time, but the web services are developing very fast) After three years of full-time software development (with focus also on software architecture, project management and microservice architecture) I definitely choose the second way (but … Read more

How to get full path of selected file on change of using javascript, jquery-ajax?

For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string: $(‘input[type=file]’).change(function () { console.log(this.files[0].mozFullPath); }); https://jsfiddle.net/SCK5A/ So don’t waste your time. edit: … Read more

JavaScript file upload size validation

Yes, you can use the File API for this. Here’s a complete example (see comments): document.getElementById(“btnLoad”).addEventListener(“click”, function showFileSize() { // (Can’t use `typeof FileReader === “function”` because apparently it // comes back as “object” on some browsers. So just see if it’s there // at all.) if (!window.FileReader) { // This is VERY unlikely, browser … Read more

How to set a value to a file input in HTML?

You cannot set it to a client side disk file system path, due to security reasons. Imagine: <form name=”foo” method=”post” enctype=”multipart/form-data”> <input type=”file” value=”c:/passwords.txt”> </form> <script>document.foo.submit();</script> You don’t want the websites you visit to be able to do this, do you? =) You can only set it to a publicly accessible web resource as seen … Read more

Sending multipart/formdata with jQuery.ajax

Starting with Safari 5/Firefox 4, it’s easiest to use the FormData class: var data = new FormData(); jQuery.each(jQuery(‘#file’)[0].files, function(i, file) { data.append(‘file-‘+i, file); }); So now you have a FormData object, ready to be sent along with the XMLHttpRequest. jQuery.ajax({ url: ‘php/upload.php’, data: data, cache: false, contentType: false, processData: false, method: ‘POST’, type: ‘POST’, // … Read more

techhipbettruvabetnorabahisbahis forumu