1

some one knows how to send a file in an ajax request? my current problem is that i have the URL of the file in a

<input type="file" />

but i don't know how to "load" the file in a variable or js object to can send it to my server

Can someone who has done this help me ? if there is any way to do it with AngularJS it would be great

iktuz
  • 11
  • 1
  • 3
  • 1
    `` is not for URLs. If you have an URL, use `` (HTML5) or plain ``. Send the URL unchanged to the server, and let the server download it. If you in fact have a *file* (and not a file URL) in a `` element, you'll use `FormData` to get the file contents into the AJAX request; you will also have to handle them at serverside, which is very language-dependent. – Amadan Nov 14 '14 at 23:15
  • possible duplicate of [File Upload using angularjs](http://stackoverflow.com/questions/18571001/file-upload-using-angularjs) – Håkan Fahlstedt Nov 14 '14 at 23:19
  • @HåkanFahlstedt duplicate - exactly like something else, especially through having been copied. This isn't a duplicate. –  Nov 14 '14 at 23:34
  • Try this: https://github.com/danialfarid/angular-file-upload –  Nov 14 '14 at 23:34
  • Thanks to all i solve it using a form and sending the post, i know this isn´t ajax but at the end was accepted for the project, thaks to all – iktuz Jan 04 '18 at 23:29

1 Answers1

1

Well you have two options to do so

  1. by converting to base64 but you can only send a max of 2mb data with it

  2. with form data, convert your data to multipart/form-data to do so you can use form data

     var formData = new FormData();
    
     formData.append('file-name', $(file-content);
    
Anonymus
  • 21
  • 3