Skip to content

Instantly share code, notes, and snippets.

@h1dia
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h1dia/15eb23cd9cc935fc0ce8 to your computer and use it in GitHub Desktop.
Save h1dia/15eb23cd9cc935fc0ce8 to your computer and use it in GitHub Desktop.
Ajaxでphpにファイルをアップロードする
<-- you should use Bootstrap3 and jQuerry 2.1.3 or up --!>
<div class="row">
<form action="javascript:;">
<input id="lefile" type="file" style="display:none">
<div class="col-xs-8 form-inline">
<input id="filepath" class="form-control" type="text">
<a class="btn btn-default" onclick="$('input[id=lefile]').click();">Browse</a>
<input type="submit" class="btn btn-primary" id="update" value="POST" />
</div>
</form>
</div>
<script>
$('input[id=lefile]').change(function() {
$('#filepath').val($(this).val().replace(/(c:\\)*fakepath\\/i, ''));
});
$(function(){
$("#update").bind("click", function(){
// loading message
var fd = new FormData();
if ( $("#lefile").val() !== '' ) {
fd.append( "file", $("#lefile").prop("files")[0] );
}
var postData = {
type : "POST",
dataType : "text",
data : fd,
processData : false,
contentType : false
};
$.ajax( "./upload.php", postData ).done(function( text ){
// success message
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment