|
楼主 |
发表于 2017-9-13 19:16:29
|
显示全部楼层
下面是负暄分享的代码,看不太懂,搞不明白应如何修改,自己的固定路径改在哪啊?
烦劳老师指导一下,谢谢谢谢!
另外请教图片上传到服务器后URL如何取得?
//调用bass实现图片上传
Model.prototype.uploadPic = function(imageData){
//用户信息,用于上传图片后保存图片链接fImage
var userid = this._UserID; //this._UserID是自己定义的变量,在页面打开时被初始化
var userBaasData = this.comp("userBaasData");
var storeFileName = "";
var imageJson = {};
storeFileName = justep.UUID.createUUID();
justep.Baas.sendRequest({
"url" : "/summer/classes", //baas目录,请根据自己的配置进行修改
"action" : "uploadPicfile",
"async" : false,
"params" : {
"picData" : imageData,
"ownerID" : userid,
"storeFileName" : storeFileName
},
"success" : function(data) {
justep.Util.hint("图片上传成功");
//修改数据库中的图片链接 这里我的字段名叫fImage,可以根据自己的字段名修改
userBaasData.setFilter("Idfilter", "fID='"+userid+"'");
userBaasData.refreshData();
if(userBaasData.getCount()>0){
var fImage = '{"ownerID":"'+userid+'","storeFileName":"'+storeFileName+'"}';
userBaasData.setValue("fImage", fImage);
userBaasData.saveData();
}
}
});
};
然后是,后台对应的图片上传baas,将图片保存在UI2/myClassmate/uploadfiles目录下,这里可以自己进行调整:
public static JSONObject uploadPicfile(JSONObject params, ActionContext context) throws SQLException, NamingException, IOException {
System.out.println("uploadPicfile is running...");
String ownerID = params.getString("ownerID");
String storeFileName = params.getString("storeFileName");
String picData = params.getString("picData");
String baasPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() ;
String docStorePath = baasPath + ".." + File.separator + ".."+ File.separator + "model" +File.separator + "UI2" +File.separator + "myClassmate" +File.separator+"uploadfiles" ;
File docstoreDir = new File(docStorePath);
if(!(docstoreDir.exists() && docstoreDir.isDirectory())){
docstoreDir.mkdirs();
}
String storePath = docStorePath + File.separator + ownerID;
File storeDir = new File(storePath);
if(!(storeDir.exists() && storeDir.isDirectory())){
storeDir.mkdirs();
}
System.out.println("后台计算的baasPath的值:"+ baasPath);
System.out.println("后台计算的docStorePath的值:"+ docStorePath);
System.out.println("后台计算的storeDir的值:"+ storeDir);
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] decodedBytes = decoder.decodeBuffer(picData);
String imgFile = storePath + File.separator + storeFileName;
FileOutputStream out = new FileOutputStream(imgFile);
out.write(decodedBytes);
out.close();
System.out.println("图片上传成功!");
} catch (Exception e) {
System.out.println("图片上传失败!");
e.printStackTrace();
}
return null;
}
|
|