Chrome 扩展 - 将 CSV 文件上传到 Dropbox 格式问题

Chrome Extension - Upload CSV File to Dropbox Format Issue

我正在构建一个 chrome 扩展,需要将 CSV 文件上传到保管箱。

除了数据在已成功上传到 Dropbox 的文件中的结构方式之外,一切正常。

我也在下载该文件的本地副本,一切看起来都很好,但在 Dropbox 上它似乎无法识别结尾字符 ("/r/n"),并将空格转换为 "%20"。这是我创建文件并将其上传到保管箱的代码的一部分:

function downloadCSV()
{

localStorage["timeStamp"]="inactive";
localStorage["ProfileViews"]="";

// create a csv file with the table data using base64
var encodedUri = encodeURI(localStorage.Table);
var file = 'data:Application/octet-stream,' + encodedUri;
var date = new Date();
// filename contains the unique user id + a timestamp of the current date with year, month, day, hours, minutes, seconds
var filename=uniqueID +"  "+date.getYear()+ "-" +date.getMonth() +"-" +date.getDate() +": " +date.getHours() + "." +date.getMinutes() +": "+date.getSeconds()+".csv";

var link = document.createElement("a");
link.setAttribute("href", file);
link.setAttribute("download", filename);
link.click();

//create a xmlhttp request to send a post request to box.com using the authenthification token
var uploadUrl = 'https://api-content.dropbox.com/1/files_put/dropbox/myfolder/'+filename;

// The Box OAuth 2 Header. Add your access token.
var headers = {
    Authorization: 'Bearer '+localStorage.authorization_token
};


$.ajax({
    url: uploadUrl,
    headers: headers,
    type: 'PUT',
    // This prevents JQuery from trying to append the form as a querystring
    processData: false,
    contentType: false,
    data: link
}).complete(function ( data ) {
    // Log the JSON response to prove this worked
    console.log(data.responseText);
});




// resets the Table variable
 localStorage["Table"]="";


}

我尝试了各种可能的编码方式,但结果还是一样。任何解决此问题的帮助将不胜感激。

尝试使用 contentType:'application/octet-stream'

我不明白您要在代码中做什么。看起来您正在使用数据的 URL 编码版本作为 href 创建超链接,但随后您将该 link 变量作为数据传递给 AJAX 请求......那最终会做什么?您应该只传递数据:

$.ajax({
    ...
    data: /* the actual data you want to upload here */
    ...
});

我无法从您的代码中判断 localStorage.Table 是否是实际的 CSV 数据。 (一条评论提到了 base64 编码?)但是无论变量在文件中包含您想要的确切 CSV 数据,都应该作为 HTTP 请求中的 data 参数传递。不要 base64 编码,不要 URL 编码等