Uploading file to BrickFTP using Dropzone

浪子不回头ぞ 提交于 2019-12-10 12:03:24

问题


I have been trying to implement file upload to BrickFTP using Dropzone, but the uploaded file won't open because it contains the WebKitFormBoundary at the top of the file content.

I'm putting method as PUT in Dropzone configuration as per BrickFTP's documentation. BrickFTP uses Amazon S3 so the files are actually being uploaded to S3. I did everything as per their documentation, and everything worked except this last problem I'm having with those extra information at the top of the uploaded file content.

Here is the Coffeescript code responsible for file upload:

brickFTPData = {}

# As per BrickFTP docs, step 1 is 
# to get the dedicated upload url for each file by sending a  
# request to REST API to indicate intent to upload a file
getUploadUri = (file) ->
   result = ""
   $.ajax
      url      : '/a/portal-dev/get-api-keys'
      type     : 'POST'
      dataType : 'json'
      data     : { file_name: file.name }
      async    : false
      success  : (data, textStatus, jqXHR) ->
         brickFTPData[file.upload.uuid] =
            file_name   : file.name
            upload_uri  : data.upload_uri
            ref         : data.ref
         result = data.upload_uri
   return result

# 3rd step is to notify the REST API that the file upload is complete.
finishUpload = (file) ->
   $.ajax
      url      : '/a/portal-dev/upload-done'
      type     : 'POST'
      dataType : 'json'
      data     :
         ref         : brickFTPData[file.upload.uuid].ref
         file_name   : brickFTPData[file.upload.uuid].file_name
      success  : (data) ->
         delete brickFTPData[file.upload.uuid]
         console.log data.status

# 2nd step is to upload the file
sampleQuoteDropzone = new Dropzone "#sampleQuoteDropzone",
   url            : '/demo'
   method         : 'PUT'
   headers        : {"Content-Type": "application/octet-stream"}
   success        : (file, request) ->
      finishUpload(file)

sampleQuoteDropzone.on 'processing', (file) ->
   upload_uri = getUploadUri(file)
   sampleQuoteDropzone.options.url = upload_uri

Upload works fine using the above code but as said when I open the uploaded file into a text editor it starts with following code:

------WebKitFormBoundaryw4bIakMBbkp7ES2E
Content-Disposition: form-data; name="file"; filename="IMG_5652.jpg"
Content-Type: image/jpeg

If I delete these lines and save the file it will work.

But this problem is not seen when uploading file using a regular ajax call outside Dropzone. Following is the working code:

$.ajax
     url: data.upload_uri
     type: 'PUT'
     contentType: 'application/octet-stream'
     data: file
     processData: false
     success: (response) ->
        finishUpload(file, data)

Can anyone please advise how to solve this problem?


回答1:


I solved the problem with following code:

sampleQuoteDropzone.on 'sending', (data, xhr, formData) ->
   _send = xhr.send
   xhr.send = ->
      _send.call(xhr, data)

This solution was actually found here. I saw this before posting this question here, but wasn't sure if this is the right problem/solution. I contacted BrickFTP and they responded fairly quickly saying this solution may work for me, and yes it certainly does.



来源:https://stackoverflow.com/questions/47134792/uploading-file-to-brickftp-using-dropzone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!