gpt4 book ai didi

javascript - knockout JS : Fileupload event

转载 作者:搜寻专家 更新时间:2023-11-01 04:26:02 24 4
gpt4 key购买 nike

我有这个用于上传文件的knockout js脚本

当用户在上传控件中选择一个文件时,这段代码会触发上传事件

Upload.html

    $(function() {
var viewModel = {
filename: ko.observable(""),
};

ko.applyBindings(viewModel);
});

<form>
<input id="upload" name="upload"
data-bind="fileUpload: { property: 'filename', url: 'http://localhost/api/upload/PostFormData' }"
type="file" />

<button id="submitUpload">Upload</button>
</form>

FileUpload.js

ko.bindingHandlers.fileUpload = {
init: function (element, valueAccessor) {
$(element).after('<div class="progress"><div class="bar"></div><div class="percent">0%</div></div><div class="progressError"></div>');
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {


var options = ko.utils.unwrapObservable(valueAccessor()),
property = ko.utils.unwrapObservable(options.property),
url = ko.utils.unwrapObservable(options.url);



if (property && url) {

$(element).change(function() {
if (element.files.length) {
var $this = $(this),
fileName = $this.val();

// this uses jquery.form.js plugin
$(element.form).ajaxSubmit({
url: url,
type: "POST",
dataType: "text",
headers: { "Content-Disposition": "attachment; filename=" + fileName },
beforeSubmit: function() {
$(".progress").show();
$(".progressError").hide();
$(".bar").width("0%")
$(".percent").html("0%");

},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + "%";
$(".bar").width(percentVal)
$(".percent").html(percentVal);

},
success: function(data) {
//$(".progress").hide();
//$(".progressError").hide();
// set viewModel property to filename
$("label[for='upload']").text(data);

bindingContext.$data[property](data);
},
error: function(jqXHR, errorThrown) {
$(".progress").hide();
$("div.progressError").html(jqXHR.responseText);
}
});
}
});
}
}

现在,我想将上传事件的触发移至提交按钮

 <button id="submitUpload">Upload</button>

如何做到这一点?现在这就是我所在的位置,我只是将上传事件移到按钮的点击事件中。但它不工作,它不会调用 API 的 ajax 请求。

  $('#submitUpload').click(function () {

if (element.files.length) {

var $this = $(element),
fileName = $this.val();
//alert(element.form);

// this uses jquery.form.js plugin
$(element.form).ajaxSubmit({
url: url,
type: "POST",
dataType: "text",
headers: { "Content-Disposition": "attachment; filename=" + fileName },
beforeSubmit: function() {
$(".progress").show();
$(".progressError").hide();
$(".bar").width("0%")
$(".percent").html("0%");

},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + "%";
$(".bar").width(percentVal)
$(".percent").html(percentVal);

},
success: function(data) {
//$(".progress").hide();
//$(".progressError").hide();
// set viewModel property to filename
$("label[for='upload']").text(data);

bindingContext.$data[property](data);
},
error: function(jqXHR, errorThrown) {
$(".progress").hide();
$("div.progressError").html(jqXHR.responseText);
}
});
}
});

最佳答案

绑定(bind)处理程序的 URL 不是只传递名称,而是从您的 ViewModel 对象传递第三个参数(fileBinaryData),然后在 KO BindingHandler 的更新方法中读取文件内容,然后在更新方法中更新第三个可观察对象(fileBinaryData)。

然后你可以在你的 View 模型中使用这个文件二进制数据

因此对于按钮绑定(bind)单击事件并访问将具有文件内容的 fileBinaryData observable。

绑定(bind)处理程序:

ko.bindingHandlers.FileUpload = {
init: function (element, valueAccessor) {
$(element).change(function () {
var file = this.files[0];
if (ko.isObservable(valueAccessor())) {
valueAccessor()(file);
}
});
},
update: function (element, valueAccessor, allBindingsAccessor) {
var file = ko.utils.unwrapObservable(valueAccessor());
var bindings = allBindingsAccessor();

if (bindings.fileBinaryData && ko.isObservable(bindings.fileBinaryData)) {
if (!file) {

bindings.fileBinaryData(null);
} else {
var reader = new window.FileReader();
reader.onload = function (e) {

bindings.fileBinaryData(e.target.result);
};
reader.readAsBinaryString(file);
}
}
}
}

HTML:

<input type="file" id="fileUpload" class="file_input_hidden" data-bind="FileUpload: spFile, fileObjectURL: spFileObjectURL, fileBinaryData: spFileBinary" /> 

View 模型:

var viewModel = {
filename: ko.observable(""),
url: ko.observable(),
spFileBinary:ko.observable(),
//Write your CLICK EVENTS
};

希望这有帮助:)

关于javascript - knockout JS : Fileupload event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17336298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com