admin管理员组文章数量:1313598
I've been working on this all day now, and I just can't get it working.
I have basically got a simple ajax request using the jQuery library and I want to send the data which I post through a mutlipart/form-data file input, however, I have tried everything I can think of.
My File upload script is in place awaiting the file name as a parameter (tried without also), but it just doesn't want to get the data from the file input box itself.
Could someone please enlighten me on how to do this without another plugin (multiple upload, etc).
Here is my jQuery Code for this bit:
function uploadTimesheets(){
$('#waiting').show();
var error = '';
var msg = '';
//Performs the Ajax Request
var data = $.ajax({
type : 'POST',
url : '/ajax/timesheet/uploadNewTimesheets.php',
dataType : 'json',
contentType : 'multipart/form-data',
data : data,
error : error,
msg : msg,
success : function(data){
if(!data){
$('#notification').removeClass(this).addClass('notification-success').html(data).show().delay(1200).fadeOut(800);
getActiveTimesheets(getSelectedPage());
}else{
$('#notification').removeClass().addClass('notification-error').html(data.msg + data.errorList).show();
alert('PHHAIL');
}
$('#waiting').hide();
function(xhr, status, errorThrown){
$('#waiting').hide();
}
}
});
}
And here is my PHP upload script:
/**
* Creates a directory in the active directory with the given folder name
*
* @author RichardC
* @param string $dirName
* @return boolean
*/
public function createDir( $dirName ) {
$docRoot = getenv('DOCUMENT_ROOT');
if (!is_dir(sprintf('%s/%s', $docRoot, $dirName))) {
$makeDir = mkdir(sprintf('%s/%s', $docRoot, $dirName));
echo sprintf('Creating a folder called \'/%s/\' ...', $dirName);
if ($makeDir) {
echo '<br />Successfully created the folder.<br />';
return true;
} else {
echo sprintf('<br /> Sorry, please create the folder manually at: %s/%s', $docRoot, $dirName);
return false;
}
}
}
/**
* Uploads either a CSV or an EXCEL file to a temporary directory
*
* @author RichardC
* @param Resource $file
* @return Boolean true/false
*/
public function upload( $filename ) {
$filename = (!isset($filename)) ? $this->file : $filename;
//Get the document root
$docRoot = getenv('DOCUMENT_ROOT');
$this->createDir('uploads');
if (($_FILES['file']['type'] == 'application/vnd.ms-excel') || ($_FILES['file']['type'] == 'application/csv') || ($_FILES['file']['type'] == 'text/csv') || ($_FILES['file']['type'] == 'text/ma-separated-values') || ($_FILES['file']['type'] == 'application/excel') &&
($_FILES["file"]["size"] < 1000000)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
if (file_exists($docRoot . "upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
$this->file = $docRoot . '/upload/' . $_FILES["file"]["name"];
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], $docRoot . "/upload/" . $_FILES["file"]["name"]);
$this->file = $docRoot . '/upload/' . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
return false;
}
//Remove the unwanted file now
$this->fileContents = file_get_contents($this->file);
@unlink($this->file);
unset($this->file);
return true;
}
If anyone can help on this, it'd be much appreciated!
I've been working on this all day now, and I just can't get it working.
I have basically got a simple ajax request using the jQuery library and I want to send the data which I post through a mutlipart/form-data file input, however, I have tried everything I can think of.
My File upload script is in place awaiting the file name as a parameter (tried without also), but it just doesn't want to get the data from the file input box itself.
Could someone please enlighten me on how to do this without another plugin (multiple upload, etc).
Here is my jQuery Code for this bit:
function uploadTimesheets(){
$('#waiting').show();
var error = '';
var msg = '';
//Performs the Ajax Request
var data = $.ajax({
type : 'POST',
url : '/ajax/timesheet/uploadNewTimesheets.php',
dataType : 'json',
contentType : 'multipart/form-data',
data : data,
error : error,
msg : msg,
success : function(data){
if(!data){
$('#notification').removeClass(this).addClass('notification-success').html(data).show().delay(1200).fadeOut(800);
getActiveTimesheets(getSelectedPage());
}else{
$('#notification').removeClass().addClass('notification-error').html(data.msg + data.errorList).show();
alert('PHHAIL');
}
$('#waiting').hide();
function(xhr, status, errorThrown){
$('#waiting').hide();
}
}
});
}
And here is my PHP upload script:
/**
* Creates a directory in the active directory with the given folder name
*
* @author RichardC
* @param string $dirName
* @return boolean
*/
public function createDir( $dirName ) {
$docRoot = getenv('DOCUMENT_ROOT');
if (!is_dir(sprintf('%s/%s', $docRoot, $dirName))) {
$makeDir = mkdir(sprintf('%s/%s', $docRoot, $dirName));
echo sprintf('Creating a folder called \'/%s/\' ...', $dirName);
if ($makeDir) {
echo '<br />Successfully created the folder.<br />';
return true;
} else {
echo sprintf('<br /> Sorry, please create the folder manually at: %s/%s', $docRoot, $dirName);
return false;
}
}
}
/**
* Uploads either a CSV or an EXCEL file to a temporary directory
*
* @author RichardC
* @param Resource $file
* @return Boolean true/false
*/
public function upload( $filename ) {
$filename = (!isset($filename)) ? $this->file : $filename;
//Get the document root
$docRoot = getenv('DOCUMENT_ROOT');
$this->createDir('uploads');
if (($_FILES['file']['type'] == 'application/vnd.ms-excel') || ($_FILES['file']['type'] == 'application/csv') || ($_FILES['file']['type'] == 'text/csv') || ($_FILES['file']['type'] == 'text/ma-separated-values') || ($_FILES['file']['type'] == 'application/excel') &&
($_FILES["file"]["size"] < 1000000)) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
if (file_exists($docRoot . "upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
$this->file = $docRoot . '/upload/' . $_FILES["file"]["name"];
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], $docRoot . "/upload/" . $_FILES["file"]["name"]);
$this->file = $docRoot . '/upload/' . $_FILES["file"]["name"];
}
}
} else {
echo "Invalid file";
return false;
}
//Remove the unwanted file now
$this->fileContents = file_get_contents($this->file);
@unlink($this->file);
unset($this->file);
return true;
}
If anyone can help on this, it'd be much appreciated!
Share Improve this question asked Sep 22, 2011 at 14:24 DarkMantisDarkMantis 1,5165 gold badges19 silver badges40 bronze badges 1- 1 I am not sure that will allow for file fields... however, there's a JQuery plugin that handles submitting forms with files: jquery.malsup./form. Another option is using a file upload plugin like uploadify.. – Michael C. Gates Commented Sep 22, 2011 at 14:29
1 Answer
Reset to default 6In order to make your multipart/formdata work, you must add some extra stuff in your ajax-request:
cache: false,
contentType: false,
processData: false,
You can easily create your data-field by doing this:
var uploadData = $("#uploadFile").prop("files")[0];
var newData = new FormData();
$.each($('#uploadFile').prop("files"), function(i, file) {
newData.append('file-'+i, file);
});
in your ajax-request you'll have to set this:
data: newData
本文标签: phpUsing jQuery to send data from a multipartformdata through ajaxStack Overflow
版权声明:本文标题:php - Using jQuery to send data from a multipartform-data through ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741949791a2406641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论