【发布时间】:2014-08-14 05:56:18
【问题描述】:
我有下面的html
This form allows you to upload a file to the server.<br>
<div class="imguploadpath" style="width:100%;height:200px;background-color:#CCC;">
<form name="myFormName" id ="myFormId" action="" method="post">
<input type="file" name="img" id = "image_pe">
<br/><br/><br/><br/>
<input id="cmdSubmit" value="Submit" type="submit" class="">
</form>
</div>
我正在尝试使用以下脚本获取文件字段值以在 php 中上传文件
jQuery( "#cmdSubmit" ).on( "click", function() {
var file = jQuery('#image_pe').val();
//我必须在 jquery ajax 中将文件属性发送到 php 文件
$.ajax({
url: 'submit.php?files',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR)
{
if(typeof data.error === 'undefined')
{
// Success so call function to process the form
submitForm(event, data);
}
else
{
// Handle errors here
console.log('ERRORS: ' + data.error);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
console.log('ERRORS: ' + textStatus);
// STOP LOADING SPINNER
}
});
return false;
});
但是var file 只返回一些临时路径c://fakepath/image.jpg
jquery和php怎么上传,需要在php文件中传表单域,我知道value()jquery函数出错了。
正确的做法是什么?
对于php文件上传,我正在考虑使用这个脚本http://www.tizag.com/phpT/fileupload.php
【问题讨论】:
-
也许这会有所帮助 [stackoverflow.com/questions/24168040/… [1]: stackoverflow.com/questions/24168040/… 希望有帮助
-
abandon.ie/notebook/simple-file-uploads-using-jquery-ajax ....这对我找到问题也有很大帮助
标签: php jquery ajax file-upload