我正在尝试创建一个将文件直接上传到dropbox.com的Chrome扩展程序。除了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();
这是将文件上传到dropbox的代码
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);
});
答案 0 :(得分:0)
查看您的代码,看起来您正在尝试传递link
(锚标记?)作为数据。我想你想要传递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);
});