Ember.js value binding with HTML5 file upload

后端 未结 2 495
耶瑟儿~
耶瑟儿~ 2021-01-01 01:58

I am not far from it to get the file upload working with Ember-data. But I do not get the value binding right. Below the relevant code.

This is the App.js

2条回答
  •  长发绾君心
    2021-01-01 02:34

    I found that using attaching a data URI to a model attribute didn't allow files more than about 60k to be uploaded. Instead I ended up writing a form data adapter for ember-data. This will upload all the model data using FormData. Sorry that it's in CoffeeScript rather than JS but it's pasted from my blog post. You can read the whole thing at http://blog.mattbeedle.name/posts/file-uploads-in-ember-data/

    `import ApplicationAdapter from 'appkit/adapters/application'`
    
    get = Ember.get
    
    FormDataAdapter = ApplicationAdapter.extend
      ajaxOptions: (url, type, hash) ->
        hash = hash || {}
        hash.url = url
        hash.type = type
        hash.dataType = 'json'
        hash.context = @
    
        if hash.data and type != 'GET' and type != 'DELETE'
          hash.processData = false
          hash.contentType = false
          fd = new FormData()
          root = Object.keys(hash.data)[0]
          for key in Object.keys(hash.data[root])
            if hash.data[root][key]
              fd.append("#{root}[#{key}]", hash.data[root][key])
    
          hash.data = fd
    
        headers = get(@, 'headers')
        if headers != undefined
          hash.beforeSend = (xhr) ->
            for key in Ember.keys(headers)
              xhr.setRequestHeader(key, headers[key])
    
        hash
    
    `export default FormDataAdapter`
    

提交回复
热议问题