即使图片上传,我也总是会收到其中一个错误。
注意: 当我离开应用程序而没有任何请求时,代码有时会运行,否则我会收到以下错误。
// Destination URL
let url = "https://***s.me/petupload.php";
// File for Upload
let targetPath = imgpath;
// File name only
let filename = imgname;
let options: FileUploadOptions = {
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
headers:{ Connection: "close"},
params : {'fileName': filename,func: "upload"}};
let fileTransfer: FileTransferObject =
this.filetransfer.create();
// Use the FileTransfer to upload the image
fileTransfer.upload(targetPath, url, options,true).then(data => {
console.log(JSON.stringify(data.response));
console.log("image sent");}, err => {
console.log(JSON.stringify(err));});
错误:
{“code”:3,“source”:“file:///data/user/0/io.ionic.starter/files/amarimg.jpg","target":"https://** * s.me / petupload.php “‘HTTP_STATUS’:空,‘身体’:空,‘例外’:” 状态: 2” }
{“代码”:3,“源极”:“文件:///data/user/0/io.ionic.starter/files/amarimg.jpg”,“目标”:“https://开头** * s.me / petupload.php “‘HTTP_STATUS’:空,‘身体’:空,‘例外’:” 预期 1596字节,但收到16384“}
在我的php中,我有以下标题
header("Access-Control-Allow-Origin: *");
header("Content-type:multipart/form-data");
header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content- Disposition, Content-Description');
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
$target_path = "/var/www/html/";//"uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']);
if(isset($_POST['func'])){
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "Upload and move success";
} else {
echo $target_path;
echo "There was an error uploading the file, please try again!";}
;}else{echo"no func";}
任何想法??
此问题已由ewizard解决
这个mimeType:“multipart / form-data”应该是这个mimeType:“image / jpeg”?你实际上可以删除mimeType ...它默认为image / jpeg也可以删除chunkedMode
然而,如果可能的话,我仍然需要使用multipart / form-data,这就是为什么我要把这个问题打开。
答案 0 :(得分:0)
此问题已由ewizard最后评论解决,但仍需要使用multipart。
答案 1 :(得分:0)
我的问题是因为我没有返回JSON,所以解析会出错:)