admin管理员组文章数量:1384246
I am trying to create a simple jquery file upload following the steps in this tutorial: / and it is fully functional and excellent. However I want to have more control and more security over what users can upload images, for example I want to be able to send a token as well. Here is my original code:
$('#images').change(function (evt) {
$('#response').text("Uploading . . .");
var amount = this.files.length;
var reader, file;
alert(formdata);
for (var i = 0; i < amount; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
appendUploadedPic(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "php/upload.php",
type: "POST",
data: formdata,
dataType:'json',
processData: false,
contentType: false,
success: function (res) {
$('#response').html(res['image_name']);
}
});
}
});
and the php is simple so far
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "../members/" . $_FILES['images']['name'][$key]);
}
}
$respond = array("filename" => $_FILES["images"]["name"][0], "type" => "image");
echo json_encode($respond);
now what I want to do is this
$.ajax({
url: "php/upload.php",
type: "POST",
data: 'token=someToken&code=upload&data='+formdata,
dataType:'json',
processData: false,
contentType: false,
success: function (res) {
$('#response').html(res);
}
});
Is it possible? If so, how would I read it from php? It its a post method i normally use a $_POST['code'] and $_POST'token'], but how would I read the images formdata object?
I am trying to create a simple jquery file upload following the steps in this tutorial: http://net.tutsplus./tutorials/javascript-ajax/uploading-files-with-ajax/ and it is fully functional and excellent. However I want to have more control and more security over what users can upload images, for example I want to be able to send a token as well. Here is my original code:
$('#images').change(function (evt) {
$('#response').text("Uploading . . .");
var amount = this.files.length;
var reader, file;
alert(formdata);
for (var i = 0; i < amount; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
appendUploadedPic(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "php/upload.php",
type: "POST",
data: formdata,
dataType:'json',
processData: false,
contentType: false,
success: function (res) {
$('#response').html(res['image_name']);
}
});
}
});
and the php is simple so far
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "../members/" . $_FILES['images']['name'][$key]);
}
}
$respond = array("filename" => $_FILES["images"]["name"][0], "type" => "image");
echo json_encode($respond);
now what I want to do is this
$.ajax({
url: "php/upload.php",
type: "POST",
data: 'token=someToken&code=upload&data='+formdata,
dataType:'json',
processData: false,
contentType: false,
success: function (res) {
$('#response').html(res);
}
});
Is it possible? If so, how would I read it from php? It its a post method i normally use a $_POST['code'] and $_POST'token'], but how would I read the images formdata object?
Share Improve this question asked Apr 12, 2013 at 22:08 lomas09lomas09 1,1044 gold badges13 silver badges29 bronze badges1 Answer
Reset to default 5If formdata
was created with FormData
, you append additional fields like this:
formdata.append(name, value);
So, if you want to add a token field and a code field, it's:
if (formdata) {
formdata.append("images[]", file);
formdata.append("token", "someToken");
formdata.append("code", "someCode");
}
However, you should be aware that this method of uploading won't be supported by a lot of browsers, namely IE6-IE9, which represents about half of all internet users, I believe.
I remend using a plugin that detects browser support and uses the best available method. I wrote this plugin for exactly that purpose:
https://github./LPology/Simple-Ajax-Uploader
There's a link to a demo at the top to try it out. If you decide to use it and run into any issues, just ping me and I can help you out. Good luck.
本文标签: phpajax Sending File formdata along with variablesStack Overflow
版权声明:本文标题:php - $.ajax Sending File formdata along with variables - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744529469a2610944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论