3

我正在嘗試創建一個將文件直接上傳到dropbox.com的Chrome擴展程序。除了csv格式以外,一切正常。下面的代碼展示了我如何創建和格式化文件,然後我自動下載它。當我打開它,它看起來不錯,問題是,當我上傳同一文件格式dropbox.com,當我後來打開它:將CSV文件上傳到dropbox.com格式問題

  • 不承認「/n」(endlines)
  • 將空格視爲'%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); 
}); 
+0

參見[數據URI方案(https://en.wikipedia.org/wiki/Data_URI_scheme),並通過'BTOA使用'base64'編碼()'或'encodeURIComponent'只適用於'localStorage [「Table」]內容。 – wOxxOm

+0

我沒有看到上傳文件到Dropbox的代碼中的任何內容。你可以分享該代碼嗎?另外,你在某個時候提到了box.com,所以我不能100%確定你正在使用哪種產品。 – smarx

+0

我只使用Dropbox。我將在下面添加上傳代碼 –

回答

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); 
});