|
wex5 开发工具有 attachmentSimple 来实现图片上传,这个非常方便。但由于我的需求是想根据需要动态创建图片上传,而 attachmentSimple 并不能动态创建。wex5 中有图片上传的 baas :
- public static JSONObject uploadImageAjax(JSONObject params, ActionContext context) throws SQLException, NamingException {
- // 参数序列化
- JSONObject jsonObj = new JSONObject();
- String image = params.getString("image"); // 拿到字符串格式的图片
- String PicName = params.getString("PicName");
- // 去掉头部
- image = image.split(",")[1];
- // 写入磁盘
- String success = "fail";
- BASE64Decoder decoder = new BASE64Decoder();
- try {
- byte[] decodedBytes = decoder.decodeBuffer(image); // 将字符串格式的image转为二进制流(biye[])的decodedBytes
- String imgFilePath = docStorePath + PicName; // 指定图片要存放的位置
- FileOutputStream out = new FileOutputStream(imgFilePath); // 新建一个文件输出器,并为它指定输出位置imgFilePath
- out.write(decodedBytes); // 利用文件输出器将二进制格式decodedBytes输出
- out.close(); // 关闭文件输出器
- success = "上传文件成功!";
- System.out.println("上传文件成功!");
- } catch (Exception e) {
- success = "上传文件失败!|" + e.getMessage();
- e.printStackTrace();
- } finally {
- jsonObj.put("success", success);
- }
- return jsonObj;
- }
复制代码
后台代码是挺清晰易懂的。只是我有一个疑问:
在这个代码中,图片是通过二进制字符串传进来的。在前端 .w 文件中,又如何把相机拍摄的、或手机本机文件作为字符串传入进来呢?
|
|