我正在尝试创建一个 chrome 扩展程序,将文件直接上传到 dropbox.com。除了 csv 格式外,一切正常。下面的代码展示了我如何创建和格式化文件,然后自动下载它。当我打开它时它看起来很好,问题是当我将相同的文件格式上传到 dropbox.com 时,当我之后打开它时:
- 无法识别“
/n
”(结束行) - 将空格视为 '
%20
' 和 - 附加在文件“
data:text/csv;charset=utf-8
”的开头。
注意:同样,如果我直接下载到我的计算机,所有格式都没有问题,只有当我将它上传到 box.com 并打开它时才会出现问题。
var csvContent = "data:text/csv;charset=utf-8,";
csvContent += localStorage["Table"];
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 encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", filename);
link.click();
这是上传文件到保管箱的代码
var uploadUrl = 'https://api-content.dropbox.com/1/files_put/dropbox/folder/'+filename;
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);
});
最佳答案
查看您的代码,您似乎正试图将 link
( anchor 标记?)作为数据传递。我想你想传递 localStorage["Table"]
,我猜这是实际 CSV 所在的位置。
所以尝试这样的事情:
$.ajax({
url: uploadUrl,
headers: headers,
type: 'PUT',
// This prevents JQuery from trying to append the form as a querystring
processData: false,
contentType: false,
data: localStorage["Table"]
}).complete(function ( data ) {
// Log the JSON response to prove this worked
console.log(data.responseText);
});
关于javascript - 将 CSV 文件上传到 dropbox.com 格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754826/