浏览代码

上传下载优化

huangbo 9 年之前
父节点
当前提交
76e7c1161a

+ 21 - 21
display-server/web/biz/js/plugin/uploaddownload.js

@ -1,15 +1,19 @@
1 1
require(["domReady!","wadeMobile", "util"], function(doc,WadeMobile) {
2 2
	var iscroll = new iScroll("content");
3
	var picDir = "picture";
3
	var picPath = "picture/my.png";
4 4
	
5
	/*选择图片*/
5
	/**
6
	 * 选择图片 
7
	 */
6 8
	$("#getPicture").tap(function() {
7 9
		WadeMobile.getPicture(function(filePath){
8 10
			$("#uploadFilePath").html(filePath);
9 11
		});
10 12
	});
11 13

12
	//上传
14
	/**
15
	 * 上传 
16
	 */
13 17
	$("#upload").tap(function() {
14 18
		var uploadFile = $("#uploadFilePath").html();
15 19
		if(!uploadFile){
@ -18,37 +22,33 @@ require(["domReady!","wadeMobile", "util"], function(doc,WadeMobile) {
18 22
		}
19 23
		/*单文件上传*/
20 24
		var params = Wade.DataMap();
21
		params.put("FILE_PATH",picDir);
22
		//服务端SESSION_ID不能为空,服务端配置verify="false"并不校验
23
		params.put("SESSION_ID",new Date().getTime());
25
		params.put("FILE_PATH",picPath);
24 26
		WadeMobile.uploadWithServlet(uploadFile, "UploadDownloadBean.upload", params.toString(), function(result){
25
			alert(result);
26 27
			var data = new Wade.DataMap(result);
27
			alert(data);
28
			$("#fileUpload").html(data.get("FILE_NMAE"));
28
			$("#remoteFilePath").html(data.get("FILE_PATH"));
29 29
		});
30 30
		/*多文件上传*/
31 31
		//有待补充
32 32
	});
33 33
	
34
	//下载
34
	/**
35
	 * 下载
36
	 * 常用相对路径,如果只有android,可使用绝对路径
37
	 */
35 38
	$("#download").tap(function() {
36
		//文件保存路径
37
		var savePath = picDir+"/my.png"; //支持绝对路径和相对路径
38 39
		var params = new Wade.DataMap();
39
40
		params.put("FILE_PATH", picDir);//下载文件的相对路径
41
		//服务端SESSION_ID不能为空,服务端配置verify="false"并不校验
42
		params.put("SESSION_ID",new Date().getTime());
43
		WadeMobile.downloadWithServlet(savePath, "UploadDownloadBean.download", params.toString(), function(result){
40
		params.put("FILE_PATH", picPath); //下载文件的路径
41
		WadeMobile.downloadWithServlet(picPath, "UploadDownloadBean.download", params.toString(), function(savePath){
42
			alert("下载成功:"+savePath);
44 43
			$("#downloadFilePath").html(savePath);
45 44
			
46
			/*var width = $("#downloadPicture").width();
45
			var width = $("#downloadPicture").width();
47 46
			var height = $("#downloadPicture").height();
48 47
			$("#downloadPicture").removeClass("e_imagePlaceHolder");
49
			$("#downloadPicture").html("<img height='" + height + "' width='" + width + "' src='" + path + "'/>");
50
			iscroll.refresh();*/
51
			alert(result);
48
			$("#downloadPicture").html("<img height='" + height + "' width='" + width + "' src='" + savePath + "'/>");
49
			iscroll.refresh();
50
		}, function(error){
51
			alert("下载错误:"+error);
52 52
		});
53 53
	});
54 54
});

+ 4 - 4
display-server/web/template/webapp/plugin/UploadDownload.html

@ -34,8 +34,8 @@
34 34
					</div>
35 35
				</li>
36 36
				<li>
37
					<div class="content content-vertical" ontap>
38
						<div class="info" id="remoteFilePath">服务器路径:</div>
37
					<div class="content content-vertical">
38
						<div class="info">服务器路径:<span id="remoteFilePath"></span></div>
39 39
					</div>
40 40
				</li>
41 41
			</ul>
@ -47,9 +47,9 @@
47 47
				<li>
48 48
					<div class="content">
49 49
						<div class="main">
50
							<div class="title">点击下载图片</div>
50
							<div class="title">图片路径</div>
51 51
							<div class="info">
52
								下载图片路径为:<span id="downloadFilePath"></span>
52
								本地路径为:<span id="downloadFilePath"></span>
53 53
							</div>
54 54
						</div>
55 55
						<div class="fn fn-2" id="download">点击下载</div>

+ 23 - 13
wade-mobile-func/src/com/wade/mobile/func/MobileNetWork.java

@ -33,6 +33,7 @@ import com.wade.mobile.frame.plugin.Plugin;
33 33
import com.wade.mobile.safe.MobileSecurity;
34 34
import com.wade.mobile.util.BusinessCache;
35 35
import com.wade.mobile.util.Constant;
36
import com.wade.mobile.util.DirectionUtil;
36 37
import com.wade.mobile.util.EscapeUnescape;
37 38
import com.wade.mobile.util.FileUtil;
38 39
import com.wade.mobile.util.FuncConstant;
@ -44,9 +45,11 @@ import com.wade.mobile.util.http.UnirestUtil;
44 45

45 46
public class MobileNetWork extends Plugin {
46 47
	private boolean hasSetSmsListener;
48
	private DirectionUtil directionUtil;
47 49

48 50
	public MobileNetWork(IWadeMobile wademobile) {
49 51
		super(wademobile);
52
		directionUtil = DirectionUtil.getInstance(context);
50 53
	}
51 54

52 55
	public void setSmsListener(JSONArray param) throws Exception {
@ -360,11 +363,11 @@ public class MobileNetWork extends Plugin {
360 363
	public void downloadWithServlet(JSONArray param) throws Exception {
361 364
		String savePath = param.getString(0);
362 365
		String dataAction = param.getString(1);
363
		IData dataParam = param.getString(2)==null?new DataMap():new DataMap(param.getString(2));
366
		IData dataParam = param.getString(2) == null ? new DataMap() : new DataMap(param.getString(2));
364 367
		downloadWithServlet(savePath, dataAction, dataParam);
365 368
	}
366 369
	
367
	public void downloadWithServlet(final String savePath, String dataAction, IData dataParam) throws Exception {
370
	public void downloadWithServlet(final String _savePath, String dataAction, IData dataParam) throws Exception {
368 371
		// TODO Auto-generated method stub
369 372
		Map<String, String> tempPostData = transPostData(dataAction, dataParam);
370 373
		final Map<String, Object> postData = new HashMap<String, Object>();
@ -373,23 +376,30 @@ public class MobileNetWork extends Plugin {
373 376
			@Override
374 377
			protected String doInBackground(String... arg0) {
375 378
				// TODO Auto-generated method stub
376
				String result = null;
377
				try{
379
				String savePath = _savePath;
380
				try {
378 381
					InputStream in = UnirestUtil.downloadByPost(MobileConfig.getInstance().getRequestUrl(), postData);
379
					FileUtil.writeFile(in, "/sdcard" + savePath);
380
					result = "下载成功";
381
				}catch(Exception e){
382
					MobileNetWork.this.error(savePath + "异常:"+e.getMessage());// 报错回调
382
					if (!savePath.startsWith(File.separator)) {
383
						savePath = directionUtil.getDirection(savePath, true);
384
					}
385
					File file = new File(savePath).getParentFile();
386
					if (!file.exists() && !file.mkdirs()) {
387
						Utility.error("创建下载文件夹失败!");
388
					}
389

390
					FileUtil.writeFile(in, savePath);
391
				} catch (Exception e) {
392
					MobileNetWork.this.error("[" + savePath + "]异常:" + e.getMessage());// 报错回调
383 393
				}
384
				return result;
394
				return savePath;
385 395
			}
386 396
			
387 397
			@Override
388
			protected void onPostExecute(String result) {
398
			protected void onPostExecute(String savePath) {
389 399
				// TODO Auto-generated method stub
390
				super.onPostExecute(result);
391
				if (result != null) {
392
					MobileNetWork.this.callback(result);// 正常回调
400
				super.onPostExecute(savePath);
401
				if (savePath != null) {
402
					MobileNetWork.this.callback(savePath);// 正常回调
393 403
				}
394 404
			}
395 405
		}.execute();

二进制
wade-mobile-library/common/wade-mobile-server.jar