douqujin2767
2016-02-16 11:57
浏览 195

cordova-plugin-file-transfer不上传文件到服务器

I'm working on cordova project that uploads photos to server. While testing this for android, success callback is called but file not uploaded to server. Destination folder in server is present with necessary permission.

Following is the Javascript section

var options = new FileUploadOptions();
options.fileKey  = "myphoto";
options.fileName = filetoupload.substr(filetoupload.lastIndexOf('/')+1);
options.mimeType = "image/jpeg";
options.chunkedMode = false;
options.headers = {
    Connection: "close"
};
var ft = new FileTransfer();
ft.upload(filetoupload, encodeURI(websrvs_base_url), win, fail, options);

In server PHP code is

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS");
header("content-type: application/json; charset=utf-8");

if($_FILES['myphoto']['size'] > 0) {
    $path = $ROOT_PATH . "mypics/";
    $tmpname1 = $_FILES['myphoto']['tmp_name'];
    $ptname1 = $_FILES['myphoto']['name'];
    move_uploaded_file($tmpname1, $path . $ptname11);

    echo 'success';
}

On checking $_FILES['myphoto']['error'] returns 7

Server has following setting regarding file upload

file_uploads - On

memory_limit - 128M

post_max_size - 64M

upload_max_filesize - 100M

Should I added any additional headers in PHP script to receive the file from app?

图片转代码服务由CSDN问答提供 功能建议

我正在研究将照片上传到服务器的cordova项目。 在为android测试这个时,调用成功回调但是文件没有上传到服务器。服务器中的 Destination文件夹存在必要的权限。

以下是Javascript部分

  var options = new FileUploadOptions(); 
options.fileKey =“myphoto”; 
options.fileName = filetoupload.substr(filetoupload.lastIndexOf('/')+ 1); 
options.mimeType  =“image / jpeg”; 
options.chunkedMode = false; 
options.headers = {
 Connection:“close”
}; 
var ft = new FileTransfer(); 
ft.upload(filetoupload,encodeURI(websrvs_base_url)  ),赢,失败,选项); 
   
 
 

在服务器PHP代码是

 &lt;?php \  nheader(“Access-Control-Allow-Origin:*”); 
header(“Access-Control-Allow-Methods:GET,POST,PUT,OPTIONS”); 
header(“content-type:application / json; charset  = utf-8“); 
 
if($ _ FILES ['myphoto'] ['size']&gt; 0){
 $ path = $ ROOT_PATH。  “mypics /”; 
 $ tmpname1 = $ _FILES ['myphoto'] ['tmp_name']; 
 $ ptname1 = $ _FILES ['myphoto'] ['name']; 
 move_uploaded_file($ tmpname1,$ path  。$ ptname11); 
 
回声'成功'; 
} 
   
 
 

检查 $ _ FILES ['myphoto'] ['错误' ] 返回7

服务器有关于文件上传的以下设置

file_uploads - On

memory_limit - 128M

post_max_size - 64M

< code> upload_max_filesize - 100M

我是否应该在PHP脚本中添加任何其他标头以从app接收文件?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhong9055 2016-02-26 06:29
    已采纳

    It turned out to be a server issue, our demo server was disk full and it didn't allow uploading photos from app.

    We will have to check the space available for /tmp folder in server as this will be used as a temporary storage before uploading the file to actual destination. Files will not upload if /tmp folder is full or if there is any limitation.

    打赏 评论

相关推荐 更多相似问题