Browse Source

文件上传容错处理

sunyj 9 years ago
parent
commit
164702d21a

+ 4 - 3
src/main/webapp/WEB-INF/views/fileUpload.html

@@ -7,17 +7,18 @@
 </head>
 <body>
 	<form method="post" enctype="multipart/form-data">
-		选择要上传的文件:<br /> <br /> <input type="file" name="file" /><br /> <br />
+		选择要上传的文件:<br /> <br /> <input id="file" type="file" name="file" /><br />
+		<br />
 	</form>
-	<input type="button" value="Upload" onclick="upload()" />
+	<input id="upload" type="button" value="Upload" />
 	<br />
 	<div id="progressDiv" hidden="true">
 		<br /> 上传进度:
 		<progress></progress>
 		<br />
 		<p id="progress">0 bytes</p>
-		<p id="info"></p>
 	</div>
+	<p id="result"></p>
 </body>
 <script src="static/lib/jquery/jquery.min.js"></script>
 <script src="static/js/upload/app.js"></script>

+ 2 - 0
src/main/webapp/WEB-INF/views/preview2.html

@@ -19,6 +19,7 @@
 			<label class="select"> <select id="scaleSelect">
 					<option value="auto" selected="selected">自动缩放</option>
 					<option value="page_width">适合页宽</option>
+					<option value="0.25">25%</option>
 					<option value="0.5">50%</option>
 					<option value="0.75">75%</option>
 					<option value="1" title="原始宽度">100%</option>
@@ -27,6 +28,7 @@
 					<option value="2">200%</option>
 					<option value="3">300%</option>
 					<option value="4">400%</option>
+					<option id="hiddenOption" hidden="true"></option>
 			</select>
 			</label>
 		</div>

+ 31 - 6
src/main/webapp/resources/js/preview2/app.js

@@ -59,6 +59,7 @@ $("#pageIndex").keypress(function(event) {
 $("#zoomOut").click(function() {
 	if (scale >= 0.2) {
 		scale = scale / 1.2;
+		changeTextOfSelectScale()
 		renderPage();
 	}
 });
@@ -67,6 +68,7 @@ $("#zoomOut").click(function() {
 $("#zoomIn").click(function() {
 	if (scale <= 5) {
 		scale = scale * 1.2;
+		changeTextOfSelectScale()
 		renderPage();
 	}
 });
@@ -115,7 +117,9 @@ $("body").keydown(function(event) {
 	}
 });
 
-// 获取窗口宽度
+/**
+ * 获取窗口宽度
+ */
 function getWindowWidth() {
 	if (window.innerWidth)
 		winWidth = window.innerWidth;
@@ -134,7 +138,9 @@ function getWindowWidth() {
 	}
 }
 
-// 发送请求,服务器端进行填充报表、生成pdf文件等操作
+/**
+ * 发送请求,服务器端进行填充报表、生成pdf文件等操作
+ */
 function loadPdfData() {
 	var loadPdfDataUrl = "print/loadPdfData" + window.location.search;
 	if (!pageIndex) {
@@ -170,7 +176,9 @@ function loadPdfData() {
 	});
 };
 
-// 下载pdf文件
+/**
+ * 下载pdf文件
+ */
 function getDocument() {
 	PDFJS.getDocument(url).then(function(pdfDoc_) {
 		// 更新页码
@@ -181,7 +189,9 @@ function getDocument() {
 	});
 }
 
-// Get page info from document, resize canvas accordingly, and render page.
+/**
+ * Get page info from document, resize canvas accordingly, and render page
+ */
 function renderPage() {
 	if (!pdfDoc) {
 		return;
@@ -222,7 +232,9 @@ function getScale(page, multipleOfWindowWidth) {
 	return multipleOfWindowWidth / (viewportWidth / winWidth);
 }
 
-// 预览前一页
+/**
+ * 预览前一页
+ */
 function prevPage() {
 	if (!pdfDoc || pageIndex <= 1) {
 		return;
@@ -232,11 +244,24 @@ function prevPage() {
 	getDocument();
 }
 
-// 预览后一页
+/**
+ * 预览后一页
+ */
 function nextPage() {
 	if (!pdfDoc || pageIndex >= pageSize) {
 		return;
 	}
 	url = url.replace(pageIndex + ".pdf", (++pageIndex) + ".pdf");
 	getDocument();
+}
+
+/**
+ * 修改缩放下拉框所显示的内容
+ */
+function changeTextOfSelectScale() {
+	var hiddenOption = document.getElementById("hiddenOption");
+	hiddenOption.removeAttribute("hidden");
+	hiddenOption.text = (scale * 100).toFixed() + "%";
+	hiddenOption.selected = true;
+	hiddenOption.hidden = true;
 }

+ 32 - 9
src/main/webapp/resources/js/upload/app.js

@@ -1,15 +1,28 @@
 var size = 0;
 
+$("#upload").click(upload);
+
 // 绑定所有type=file的元素的onchange事件的处理函数
 $(':file').change(function() {
-	var file = this.file;
-	name = file.name;
-	size = file.size;
-	type = file.type;
-	url = window.URL.createObjectURL(file);
+	var file = this.value;
+	if (file) {
+		name = file.name;
+		size = file.size;
+		type = file.type;
+		// url = window.URL.createObjectURL(file);
+	}
 });
 
+/**
+ * 上传文件
+ */
 function upload() {
+	var value = document.getElementById("file").value;
+	if (!value) {
+		$('#progressDiv').css("display", "hidden");
+		$("#result").html("文件为空,无法进行上传!");
+		return;
+	}
 	// 创建FormData对象,初始化为form表单中的数据
 	var formData = new FormData($('form')[0]);
 	var userName = getParameter("userName");
@@ -36,7 +49,7 @@ function upload() {
 			return myXhr;
 		},
 		success : function(result) {
-			$("#result").html(result.data);
+			$("#result").html(result);
 		},
 		contentType : false,
 		processData : false
@@ -44,7 +57,11 @@ function upload() {
 
 }
 
-// 上传进度回调函数:
+/**
+ * 上传进度回调函数
+ * 
+ * @param e
+ */
 function progressHandlingFunction(e) {
 	if (e.lengthComputable) {
 		$('progress').attr({
@@ -58,8 +75,14 @@ function progressHandlingFunction(e) {
 	}
 }
 
-// 获取链接参数
-var getParameter = function(key) {
+/**
+ * 获取链接参数
+ * 
+ * @param key
+ *            参数名
+ * @returns 参数值
+ */
+function getParameter(key) {
 	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
 	var r = window.location.search.substr(1).match(reg);
 	if (r != null)