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

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

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧