我编写了文件上传代码,可以很好地上传文件并将其保存在文件夹中。我已经包含一个功能,允许用户加载 PDF 文件的 URL,并且应该上传和保存来自 URL 的文件。编码:
function loadURL(box) {
var box = dhtmlx.modalbox({
title: "Load URL",
text: "<div id='form_in_box'><div>Enter the URL of PDF file <hr/><div><div><textarea id='file' style='width: 400px; height: 27px;'></textarea></div><span class='dhtmlx_button'><input type='submit' value='Load URL' style='width: 86px' onclick='save_file(this)'></span><span class='dhtmlx_button'><input type='button' value='Cancel' onclick='close_file(this)' style='width:80px;'></span></label></div></div>",
width: "300px"
})
}
function save_file(box) {
var file = document.getElementById('file');
if (file.value == "") {
alert("Choose a file to upload");
return false;
}
dhtmlx.modalbox.hide(box);
var fd = new FormData();
fd.append('file', file.files[0]);
var xhr = new XMLHttpRequest();
xhr.open('POST', '/FileUpload/Upload', true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
alert('File successfully uploaded to the server');
}
};
xhr.send(fd);
如果我将上面的代码用于 load_URL,则会出现错误:TypeError: file.files is undefined fd.append('file', file.files[0]);