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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同