file转化为binary对象发送给后台

具体代码如下:

function filechange(e) {
				var file = $('#filed').get(0).files[0];
				var fileSize = file.size,
					fileType = file.type;
				if (file.name.lastIndexOf('.') == -1) {
					$.dialog({
						content: '路径不正确',
						time: 2000
					});
					return false;
				}
				var AllImgExt = ".jpg|.jpeg|.gif|.bmp|.png|.swf|";
				var extName = file.name.substring(file.name.lastIndexOf(".")).toLowerCase();
				if (AllImgExt.indexOf(extName + "|") == -1) {
					$.dialog({
						content: '非法图片格式',
						time: 2000
					});
					return false;
				}
				if (fileSize < 3145728) {
					if (window.FileReader) {
						var reader = new FileReader();
						reader.readAsBinaryString(file);
						reader.onload = function(e) {
							var formData = new FormData();
							formData.append("imgs", file);
							$.ajax({
								url: "",//后台地址
								type: 'post',
								contentType: "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",//格式转换为binary
								data: formData,
								processData: false, // 使数据不做处理
								contentType: false,
								dataType: 'json',
								success: function(d) {

								}
							})
						}
					}
				} else {
					$.dialog({
						content: '图片大小超过限制',
						time: 2000
					});
					return false;
				}

			}

  

posted @ 2021-01-05 16:05  林恒  阅读(6460)  评论(0编辑  收藏  举报