Skip to content

Instantly share code, notes, and snippets.

@travisfont
Created July 14, 2015 16:46
Show Gist options
  • Select an option

  • Save travisfont/aec034e0706555a2b50e to your computer and use it in GitHub Desktop.

Select an option

Save travisfont/aec034e0706555a2b50e to your computer and use it in GitHub Desktop.
jquery File Upload
<!DOCTYPE html>
<html>
<head>
<title>File Uploader Example</title>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
function submitForm()
{
var fd = new FormData(document.getElementById("fileinfo"));
fd.append("label", "WEBUPLOAD");
$.ajax(
{
url: "upload.php",
type: "POST",
data: fd,
enctype: 'multipart/form-data',
processData: false,
contentType: false
}).done(function (data)
{
var response = JSON.parse(data);
if (response.success !== undefined && response.success == true)
{
$('#file').val('');
$('#file_upload').append('<a href="/uploads/'+response.file.full+'" target="_new">'+response.file.short+'</a><br/>');
}
});
return false;
}
</script>
</head>
<body>
<form method="post" id="fileinfo" name="fileinfo" onsubmit="return submitForm();">
<!-- <label>Select a file:</label><br> -->
<input type="file" name="file" id="file" required />
<div id="file_upload"></div>
<p><input type="submit" value="Upload" /></p>
</form>
<div id="output"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment