【问题标题】:Ionic 2/3 filetransfer upload errorIonic 2/3 文件传输上传错误
【发布时间】:2018-05-13 21:13:30
【问题描述】:

即使图像已上传,我也总是遇到这些错误之一。

注意: 当我让应用程序运行一段时间而没有任何请求时,代码有时会起作用,否则我会收到以下错误。

// 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”:null,“body”:null,“异常”:"state: 2”}

{“代码”:3,“来源”:“file:///data/user/0/io.ionic.starter/files/amarimg.jpg”,“目标”:“https://** *s.me/petupload.php",“http_status”:null,“body”:null,“异常”:"预期 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";}

有什么想法吗??

这个问题已经被精灵解决了

这个 mimeType: "multipart/form-data" 应该是这个 mimeType: "image/jpeg" 吗?您实际上可以删除 mimeType ...它默认为 image/jpeg 也可能删除 chunkedMode

但是,如果可能的话,我仍然需要使用 multipart/form-data,这就是我让这个问题保持开放的原因。

【问题讨论】:

  • 你有代码吗?
  • 欢迎来到 SO。请遵循提出好问题以获得好答案的准则:)
  • 刚刚添加了代码并编辑了问题
  • 你的php代码呢?从错误的样子来看......它可能来自那里......你有任何流浪的echo声明吗?你看过php的日志吗?
  • 这个mimeType: "multipart/form-data"应该是这个mimeType: "image/jpeg"吗?您实际上可以删除 mimeType...它默认为 image/jpeg 也可能删除 chunkedMode

标签: cordova ionic-framework ionic2 ionic3


【解决方案1】:

我的问题是因为我没有返回一个JSON,所以解析会出错:)

【讨论】:

    【解决方案2】:

    这个问题已经被 ewizard last comment 解决了,但是仍然需要使用 multipart。

    【讨论】:

      猜你喜欢
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 2019-05-12
      相关资源
      最近更新 更多