這裏是一個上傳按鈕,我在我的應用程序中使用:它建立了一個輸入按鈕,自動上傳更改。
{{view App.UploadButton groupBinding="model"}}
App.UploadButton = Ember.View.extend({
tagName: 'input',
attributeBindings: ['type'],
type: 'file',
originalText: 'Upload Finished Product',
uploadingText: 'Busy Uploading...',
newItemHandler: function (data) {
var store = this.get('controller.store');
store.push('item', data);
},
preUpload: function() {
var me = this.$(),
parent = me.closest('.fileupload-addbutton'),
upload = this.get('uploadingText');
parent.addClass('disabled');
me.css('cursor', 'default');
me.attr('disabled', 'disabled');
},
postUpload: function() {
var me = this.$(),
parent = me.closest('.fileupload-addbutton'),
form = parent.closest('#fake_form_for_reset')[0],
orig = this.get('originalText');
parent.removeClass('disabled');
me.css('cursor', 'pointer');
me.removeAttr('disabled');
form.reset();
},
change: function (e) {
var self = this;
var formData = new FormData();
// This is just CSS
this.preUpload();
formData.append('group_id', this.get('group.id'));
formData.append('file', this.$().get(0).files[0]);
$.ajax({
url: '/file_upload_handler/',
type: 'POST',
//Ajax events
success: function (data) { self.postUpload(); self.newItemHandler(data); },
error: function() { self.postUpload(); alert('Failure'); },
// Form data
data: formData,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
}
});
什麼部分的代碼不能正常工作?這完全有可能。 – Kingpin2k
當我選擇圖像,並點擊上傳沒有任何反應 – monk
在控制檯上沒有錯誤什麼都沒有..我有困難的時候這東西geting – monk