doutui8842 2017-01-16 17:54
浏览 21
已采纳

使用PHP或jQuery提交动态生成的文件

I am trying to interact with the "API" at another company. Their API is actually a file upload form which asks for a CSV file. It's a standard HTML form with an element like so:

<input type="file" id="inputFileUploadFile" name="UploadFile">

I have a page where a user can view a set of table data. I am looking for a way to have a one-button solution where the user can look at the table data and submit it as a file to this so-called API.

Generating the data in CSV format is easy enough. The question, specifically, is how do I then get that data into the form element and submit it?

I can either use jQuery to manipulate and submit a hidden HTML form, or I can use PHP to submit the form directly. An answer in either format works.

  • 写回答

2条回答 默认 最新

  • dongqiongjiong4740 2017-01-16 18:04
    关注

    One approach is to use FormData Class . Below find below a sample code . You need to call this inside the function when you are calling the post on button click etc.

            --- add  enctype to form tag----  
           <form id="upload_form" enctype="multipart/form-data">
    
            -- on submit create a new object assing key value pairs and post it .
    
            var formData = new FormData($('#upload_form')[0]);
    
            formData.append('key1', 'val1');
            formData.append('key2', 'val2');
            // Main magic with files here
            formData.append('image', $('input[type=file]')[0].files[0]); 
    
    
            $.ajax({
                url: 'Your url here',
                data: formData,
                type: 'POST',
                // THIS MUST BE DONE FOR FILE UPLOADING
                contentType: false,
                processData: false,
                // ... Other options like success and etc
            })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题