为此,您必须使用自己的方法服务器端。您可以使用 RPC 方法、数据类方法或 HTTP 处理程序。
我在这里向您展示的解决方案使用最后一个,HTTP 处理程序。
您必须发送在 fileUpload 中选择的文件,然后在服务器端方法中的服务器端执行该过程。
这是我的客户端代码
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;
}
您可以在此处看到我正在处理我的文件。在这里我要做的是在位置<PROJECT_PATH>/DataFolder/Upload/ 上创建文件,然后我必须使用BinaryStream 在新创建的文件中写入内容。如果该文件已存在同名文件,我添加一个计数器,该计数器会递增,直到不存在带有数字的文件名 concat。
所以在这里你可以对文件做任何你想做的事情。您还可以提供一个 ID,然后为实体保存文件。
另一个解决方案是,就像 Issam 所说,在图像属性上使用 validate 事件,那么您应该能够在事件内部处理文件。
这还取决于您要对文件执行的过程?