Quantcast
Channel: Techathon | Techathon
Viewing all articles
Browse latest Browse all 36

How to upload file using ajax

$
0
0

Some year ago it is not possible to upload file using AJAX but now we can upload files or images using ajax

Use below code:

<form enctype="multipart/form-data" method="post" name="fileinfo">
  <label>Your email address:</label>
  <input type="email" autocomplete="on" autofocus name="userid" placeholder="email" required size="32" maxlength="64" /><br />
  <label>Custom file label:</label>
  <input type="text" name="filelabel" size="12" maxlength="32" /><br />
  <label>File to stash:</label>
  <input type="file" name="file" required />
</form>
<script>
var fd = new FormData(document.getElementById("fileinfo"));
fd.append("CustomField", "This is some extra data");
$.ajax({
  url: "stash.php",
  type: "POST",
  data: fd,
  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
});
</script>

Browser compatibility Desktop

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 7+ 4.0 (2.0) 10+ 12+ 5+
append with filename (Yes) 22.0 (22.0) ? ? ?

 

Browser compatibility Mobile

Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 3.0 ? 4.0 (2.0) ? 12+ ?
append with filename ? ? 22.0 (22.0) ? ? ?

The post How to upload file using ajax appeared first on Techathon.


Viewing all articles
Browse latest Browse all 36

Trending Articles