2017-01-24 48 views
1

我試圖讓用戶上傳一個15 MB文件到我的網站,從那裏文件應該發佈到我的Web服務,接收響應(一個pdf文件)並將其提供給用戶,以便他可以下載它。允許用戶在上傳文件後下載生成的pdf文件

不過,我在一個網址結尾是這樣,沒有及時下載任何東西,只是一個404錯誤:http://localhost:10080/Download?file=%PDF-1.4%%EF%BF%BD%EF%B (etc)

幾點:

  • 首先,我要壓縮文件
  • 因爲前面點的,我使用Ajax來發布文件

Ajax代碼

$("#file").on("change", function(evt) { 

     var files = evt.target.files; 

     /* Create zip file representation */ 
     var zip = new JSZip(); 
     /* Name, content */ 
     zip.file("data.zip", files[0]); 

     zip.generateAsync({ 
      compression: 'DEFLATE', 
      type: 'blob' 
     }).then(function(zc) { // Function called when the generation is complete 
      /* Create file object to upload */ 
      var fileObj = new File([zc], "compressed-data"); 

      /* form data oject */ 
      var formData = new FormData(); 
      /* $('#file')[0].files[0] */ 
      formData.append('attachments', fileObj); 

      $.ajax({ 
       type: "POST", 
       url: $("form#data").attr("action"), 
       data: formData, 
       contentType: false, 
       processData: false, 
       success: function (returnValue, textStatus, jqXHR) { 
        window.location = '/Download?file=' + returnValue; 
       } 
      }) 

Python的網頁代碼

def post(self): 
    attachments = self.request.POST.getall('attachments') 
    #handle the attachment 
    _attachments = [{'content': f.file.read(), 
        'filename': f.filename} for f in attachments] 

    # Use the App Engine Requests adapter. This makes sure that Requests uses 
    # URLFetch. 
    requests_toolbelt.adapters.appengine.monkeypatch() 

    #web service url 
    url = 'http://localhost:8080' 

    files = {'attachments': _attachments[0]["content"]} 

    resp = requests.post(url, files=files) 

    self.response.headers[b'Content-Type'] = b'application/pdf; charset=utf-8' 
    self.response.headers[b'Content-Disposition'] = b'attachment; filename=report.pdf' 
    self.response.out.write(resp.content) 

Python的Web服務代碼

@app.route('/', methods=['POST']) 
def hello(): 

    #the attached ZIPPED raw data 
    f = request.files["attachments"] 
    #to unzip it 
    input_zip = ZipFile(f) 
    #unzip 
    data = [input_zip.read(name) for name in input_zip.namelist()] 

    generator = pdfGenerator(io.BytesIO(data[0])) 
    return generator.analyzeDocument() 

的PDF生成器使用ReportLab的,在 io.BytesIO()寫入PDF,並返回它output = self.buff.getvalue()

1.-我在做什麼錯誤的window location事情?

2.-我做的文件類型有問題嗎?

我已經兩天了,現在我需要幫助。

感謝。

回答

1

However, I'm ending in a URL like this with no prompt to download anything, just a 404 error: http://localhost:10080/Download?file=%PDF-1.4%%EF%BF%BD%EF%B (etc)

這就是你問的window.location = '/Download?file=' + returnValue;什麼,你將用戶重定向到/Download

當您致電您的服務時,您應該要求Blob響應。然後,使用saveAs (or FileSaver.js, a polyfill)觸發下載。

據我所知,$.ajax不允許你下載開箱即用的二進制內容(它會嘗試從UTF-8解碼你的二進制文件並破壞它)。使用jQuery插件(如jquery.binarytransport.js)或直接使用xhr。

$.ajax({ 
    type: "POST", 
    url: $("form#data").attr("action"), 
    data: formData, 
    contentType: false, 
    processData: false, 
    dataType: 'binary',      // using jquery.binarytransport.js 
    success: function (returnValue, textStatus, jqXHR) { 
     // Default response type is blob 
     saveAs(returnValue, "result.pdf"); // using FileSaver.js 
    } 
}) 
+0

MASTER!當我讀到它時,有幾件事情在我腦海中並不合適,並且有點懷疑。但嘗試後,像魅力一樣工作!非常感謝你。但是,我不完全明白爲什麼我必須使用這兩個庫才能使其正常工作。 – mclzc

+0

當您將zip文件「POST」到您的後端時,您想要做2件事:1 /獲取二進制響應和2 /觸發下載。 1 /需要一個jQuery插件('$ .ajax'自己破壞二進制內容)或一個原始的'XMLHttpRequest'與'responseType =「blob」'(你已經使用jQuery,所以我使用了第一個解決方案)。 對於2 /,目前還沒有一個API可以在每個瀏覽器上實現。 'FileSaver.js'封裝了'saveAs'中的不同方式。 –

相關問題