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

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64