Angularjs 使用角形wakanda进行文件下载
我试图添加一个文件上传下降,即使与角Wakanda,但真的不知道从哪里开始 带有文件输入元素的Image Upload()正在工作,但我想在上传文件后处理文件服务器端:意思是将文件上传到服务器目录,在成功上传文件后调用服务器方法 任何开始的示例或方向都将不胜感激。您可以根据需要尝试验证或保存事件。Angularjs 使用角形wakanda进行文件下载,angularjs,file-upload,wakanda,Angularjs,File Upload,Wakanda,我试图添加一个文件上传下降,即使与角Wakanda,但真的不知道从哪里开始 带有文件输入元素的Image Upload()正在工作,但我想在上传文件后处理文件服务器端:意思是将文件上传到服务器目录,在成功上传文件后调用服务器方法 任何开始的示例或方向都将不胜感激。您可以根据需要尝试验证或保存事件。 它们允许您在保存文件之前触发一个方法。为此,您必须使用自己的方法服务器端。您可以使用RPC方法、dataclass方法或HTTP处理程序 我在这里展示的解决方案使用最后一个,HTTP处理程序 您必须在
它们允许您在保存文件之前触发一个方法。为此,您必须使用自己的方法服务器端。您可以使用RPC方法、dataclass方法或HTTP处理程序 我在这里展示的解决方案使用最后一个,HTTP处理程序 您必须在fileUpload中发送文件choosen,然后在服务器端的服务器端方法中执行该过程 这是我的客户端代码
function uploadFiles() {
return new Promise(function(resolve,refuse){
var fileInputs = $('#editorsDiv input[type="file"]'),
fd = new FormData(),
atLeastOneFile = false;
fileInputs.each(function(index,value){
if(this.files.length !== 0){
fd.append(this.id,this.files[0],this.files[0].name);
atLeastOneFile = true;
}
});
if(atLeastOneFile){
$.ajax({
url: '/uploadFile',
type: 'POST',
processData: false, // important
contentType: false, // important
dataType : 'json',
data: fd,
success:function(evt){
app.data.templateImages = evt;
resolve(true);
}
});
}else{
resolve(true);
}
});
}
这个方法返回一个承诺,但你并不真的需要它
这是我在服务器端的代码
/*
* This method can handle the upload of files send via http request
* The files are stored in the DateFolder/uploadFiles
* It return an JSON containing the id and the name of the fileuploaded. The name can change if there is an existing file with the same name.
*
*/
function uploadFile(request,response){
var i,
j=1,
nameTemp,
files=[],
returnJSON = [],
newName;
for(i=0;i<request.parts.length;i++){
files.push(new File(getFolder('path')+'/DataFolder/uploadFiles/'+request.parts[i].fileName.replace(/\s/g,'_')));
returnJSON[i]={};
returnJSON[i].name = request.parts[i].name
returnJSON[i].value = request.parts[i].fileName;
}
for(i=0;i<files.length;i++){
j=1;
if(!files[i].exists){
myBinaryStream = BinaryStream(files[i],'Write');
myBinaryStream.putBlob(request.parts[i].asBlob);
myBinaryStream.close();
}else{
while(files[i].exists){
nameTemp = files[i].name.replace(/\s/g,'_');
files[i] = new File(getFolder('path')+'/DataFolder/uploadFiles/'+files[i].nameNoExt.replace(/\s/g,'_')+j+'.'+files[i].extension);
newName = files[i].name;
if(files[i].exists){
files[i] = new File(getFolder('path')+'/DataFolder/uploadFiles/'+nameTemp);
}
j++;
}
myBinaryStream = BinaryStream(files[i],'Write');
myBinaryStream.putBlob(request.parts[i].asBlob);
myBinaryStream.close();
returnJSON[i].value = newName;
}
}
returnJSON = JSON.stringify(returnJSON);
response.contentType = 'application/json';
return returnJSON;
}
/*
*此方法可以处理通过http请求发送的文件的上载
*这些文件存储在DateFolder/uploadFiles中
*它返回一个JSON,其中包含上传文件的id和名称。如果存在同名的现有文件,则名称可能会更改。
*
*/
函数上载文件(请求、响应){
var i,
j=1,
nameTemp,
文件=[],
returnJSON=[],
新名称;
对于(i=0;i要在html元素上添加拖放支持,请在上述答案中添加以下代码:
将删除文件的验证添加到uploadFiles()
。如果没有dropppedFiles
请使用fileInputs
:
// file upload
$scope.uploadFiles = function(droppedFiles) {
return new Promise(function(resolve, refuse) {
var fileInputs = $('input[type="file"]');
var formData = new FormData();
var atLeastOneFile = false;
// validate if dropzone or file input
if (droppedFiles) {
for (var i = 0; i < droppedFiles.length; i++) {
formData.append('dropzone', droppedFiles[i], droppedFiles[i].name);
atLeastOneFile = true;
};
}
else {
fileInputs.each(function(index, value) {
if (this.files.length !== 0) {
formData.append('form', this.files[0], this.files[0].name);
atLeastOneFile = true;
}
});
}
if (atLeastOneFile) {
$.ajax({
url: '/uploadFile',
type: 'POST',
processData: false, // important
contentType: false, // important
dataType: 'json',
data: formData,
success: function(evt) {
resolve(true);
}
});
}
else {
resolve(true);
}
});
};
// dropzone for file upload
$("#drop-box").on('drag dragstart dragend dragover dragenter dragleave drop', function(e) {
e.preventDefault();
e.stopPropagation();
})
.on('dragover dragenter', function() {
$(this).addClass('is-dragover');
})
.on('dragleave dragend drop', function() {
$(this).removeClass('is-dragover');
})
.on('drop', function(e) {
$scope.uploadFiles(e.originalEvent.dataTransfer.files);
});