dongqiancui9194 2019-01-25 12:44
浏览 437
已采纳

我需要通过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.

  • 写回答

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,
              ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥30 python安卓开发
  • ¥15 使用R语言GD包一直不出结果