dongqiancui9194
2019-01-25 12:44
浏览 327

我需要通过SFTP传递Ajax来上传文件(JS + PHP)

I want the user to insert in my page a PDF or image file. Then i want to upload that file via SFTP with PHPSECLIB. I want to pass the file via ajax with some more data.

        var file = this.files[0];
        alert('You have chosen the file ' + file.name);
        var data = new FormData();                
        data.append('file', file);
        var url_string = window.location.href;
        var url = new URL(url_string);
        var id = url.searchParams.get("id");
        var url="Invoice/uploadInvoice"                
        console.log(data);  
        console.log(file);
        $.ajax({
            url: "/SPI/Invoice/uploadInvoice", // point to server-side PHP script 
            dataType: 'text',  // what to expect back from the PHP script, if anything
            cache: false,
            contentType: false,
            processData: false,
            data: {file : data , id:id},                           
            type: 'POST',
            success: function(php_script_response){
                alert(php_script_response); // display response from the PHP script, if any
            }
        });
});

I expect someting in POST in function uploadInvoice and its output is NULL.

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

我希望用户在我的页面中插入PDF或图像文件。 然后我想通过上传文件 带有PHPSECLIB的SFTP。 我想通过带有更多数据的ajax传递文件。

  var file = this.files [0]; 
 alert('你选择了' 文件'+ file.name); 
 var data = new FormData();  
 data.append('file',file); 
 var url_string = window.location.href; 
 var url = new URL(url_string); 
 var id = url.searchParams.get(“id”)  ; 
 var url =“Invoice / uploadInvoice”
 console.log(data);  
 console.log(file); 
 $ .ajax({
 url:“/ SPI / Invoice / uploadInvoice”,//指向服务器端PHP脚本
 dataType:'text',//什么到 期待从PHP脚本返回,如果有的话,n缓存:false,
 contentType:false,
 processData:false,
 data:{file:data,id:id},
 type:'POST',\  n success:function(php_script_response){
 alert(php_script_response); //显示来自PHP脚本的响应,如果有的话
} 
}); 
}); 
   \  n 
 

我希望函数uploadInvoice中有一些POST,其输出为NULL。

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

1条回答 默认 最新

  • dongzhang6544 2019-02-05 12:16
    已采纳

    What i have done here is getting the file by elementID and getting the first and only file at position 0 from the input.

            var url_string = window.location.href;
            var url = new URL(url_string);
            var id = url.searchParams.get("id");
            var url="Invoice/uploadInvoice"            
            var form_data = new FormData();
            form_data.append("id", id);
            form_data.append("nrinvoice" , document.getElementById('nrinvoice').value);
            form_data.append("file", document.getElementById('input-excel').files[0]);
            $.ajax({
                url:"<?php echo base_url('/Invoice/uploadInvoice') ?>/",
                method:"POST",
                data: form_data,
                contentType: false,
                cache: false,
                processData: false,
              ....
    
    打赏 评论

相关推荐 更多相似问题