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

5
  • 1
    <input type="file"> is not for URLs. If you have an URL, use <input type="url"> (HTML5) or plain <input type="text">. 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 <input type="file"> 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. Commented Nov 14, 2014 at 23:15
  • possible duplicate of File Upload using angularjs Commented Nov 14, 2014 at 23:19
  • @HåkanFahlstedt duplicate - exactly like something else, especially through having been copied. This isn't a duplicate. Commented Nov 14, 2014 at 23:34
  • Try this: github.com/danialfarid/angular-file-upload Commented Nov 14, 2014 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 Commented Jan 4, 2018 at 23:29

1 Answer 1

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);
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.