我的目标是通过 AJAX将图像上传到PHP。由于我在其他线程上的输入,我设法将文件转到PHP端。现在,我希望从返回到AJAX的答案是一个JSON。
我可以看到我的PHP创建了JSON并将其发回,但是出于某些原因(我怀疑它是一个PHP.INI设置),AJAX对象在其responseText中包含了一些HTML:
"<br /> <b>Notice</b>: Unknown: file created in the system's temporary directory in <b>Unknown</b> on line <b>0</b><br /> "{\u0022status\u0022:\u0022success\u0022,\u0022fileUploaded\u0022:true}""我在AJAX端的代码如下所示:
var pictureFormData = new FormData();
pictureFormData.append('pictureFile', $(#[input_type_file_on_DOM_id])[0].files[0]);
$.ajax({
url: [my php to integrate image].php,
data: pictureFormData,
type:"post",
contentType:false,
processData:false,
cache:false,
dataType:"json",
success:function(res){
console.log('AJAX SUCCESS');
console.info(res);
},
error:function(err){
console.log('AJAX ERR');
console.info(err);
},
timeout:function(){
console.log('AJAX TIMEOUT');
},
complete: function(){
console.log('AJAX COMPLETE');
}
});在我的PHP方面,处理AJAX调用的控制器如下所示:
class AjaxImageController extends Controller
{
public function indexAction(Request $request)
{
$file = $request->files->get('pictureFile');
$status = array('status' => "success","fileUploaded" => false);
// If a file was uploaded
if(!is_null($file)){
// generate a random name for the file but keep the extension
$filename = uniqid().".".$file->getClientOriginalExtension();
$path = "/tmp";
$file->move($path,$filename); // move the file to a path
$status = array('status' => "success","fileUploaded" => true);
}
$data = json_encode($status);
$response = new JsonResponse();
$response->setData($data);
dump($response);
return $response;
}
}正如您所看到的,我在发送回dump(response)之前做了一个。其内容如下:
JsonResponse {#719 ▼
#data: ""{\u0022status\u0022:\u0022success\u0022,\u0022fileUploaded\u0022:true}""
#callback: null
#encodingOptions: 15
+headers: ResponseHeaderBag {#722 ▼
#computedCacheControl: array:2 [▼
"no-cache" => true
"private" => true
]
#cookies: []
#headerNames: array:2 [▼
"cache-control" => "Cache-Control"
"content-type" => "Content-Type"
]
#headers: array:2 [▼
"cache-control" => array:1 [▼
0 => "no-cache, private"
]
"content-type" => array:1 [▼
0 => "application/json"
]
]
#cacheControl: []
}
#content: ""{\u0022status\u0022:\u0022success\u0022,\u0022fileUploaded\u0022:true}""
#version: "1.0"
#statusCode: 200
#statusText: "OK"
#charset: null
}因此,我不明白为什么responseText从:"<br /> <b>Notice</b>: Unknown: file created in the system's temporary directory in <b>Unknown</b> on line <b>0</b><br />"开始就错误地包含了HTML,然后触发了AJAX调用的error函数。
一些更新:我注意到以下内容:
在客户端,当我将pictureFormData.append('pictureFile', $(#[input_type_file_on_DOM_id])[0].files[0]);嵌入到发送给SYMFONY PHP的内容中时:我在控制器中注意到传入的$request文件显示在FileBag对象中:
Request {#9 ▼
+attributes: ParameterBag {#12 ▶}
+request: ParameterBag {#10 ▶}
+query: ParameterBag {#11 ▶}
+server: ServerBag {#16 ▶}
+files: FileBag {#14 ▼
#parameters: array:1 [▼
"pictureFile" => UploadedFile {#15 ▼
.... all files data
}
]
}
...
},然后我得到了我描述的错误。
如果我取走从客户端发送的文件,并检查控制器,$request如下所示:
Request {#9 ▼
... +files: FileBag {#14 ▼
#parameters: []
}
...
}在这种情况下,错误不会出现。因此,我怀疑FileBag对象上的操作会以某种方式抛出一个echo。
发布于 2017-02-03 13:17:25
您的响应包含HTML,因为php抛出一个错误。如果你纠正它,那么它的回报将是好的。
文件似乎不存在。
如果您没有看到服务器上的文件,或者XHR请求中的文件,那么您需要在表单上添加enctype="multipart/form-data"。
如果表单和XHR请求没有问题,那么尝试上传您的文件,并将根目录添加到上载目录中。
# app/config/config.yml
# ...
parameters:
brochures_directory: '%kernel.root_dir%/../web/uploads/brochures'和
$file->move(
$this->getParameter('brochures_directory'),
$fileName
);或者就在你的控制器里
$root = $this->get('kernel')->getRootDir();
$path = $root."/tmp";
$file->move($path,$filename); // move the file to a path发布于 2017-02-03 16:53:59
这个问题是由于PHP.INI设置造成的:
文件夹:upload_tmp_dir = path到php\php 7\uploadtemp是在PHP.INI中设置的。
“php\php 7\”的路径已经存在,但没有文件夹"uploadtemp“,因此无法创建它。
我在"path \php 7\“中创建了文件夹"uploadtemp”,并解决了这个问题。
https://stackoverflow.com/questions/42022051
复制相似问题