将input type="file" 类型的图片文件转成base64
我想要的效果呢是我在后台选了图片然后转换成base64进制并传值到另一个页面
这是我的js
这是我的HTML
我该如何把它传过去呢 我那段注释的js代码是穿换成base64进制的代码,但是有了这段代码我页面就跳转不过去,报了一个错误:Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
所以我想请教一下各位 我该怎么把file转换成base64进制并传值到另一个页面
这是我的js
<script type="text/javascript">
$(function() {
$("#yulan")
.click(
function() {
var brand = $("input[name='brand']").val();
var carmodel = $("input[name='carmodel']").val();
var oenumber = $("input[name='oenumber']").val();
var directionid = $("input[name='directionid']")
.val();
var year = $("input[name='year']").val();
var content = $("textarea[name='content']").val();
var url = "${pageContext.request.contextPath}/PreviewProduct?brand="
+ brand
+ "&content="
+ content
+ "&directionid="
+ directionid
+ "&year="
+ year
+ "&carmodel="
+ carmodel
+ "&oenumber=" + oenumber;
/* var file = $("#file1");
alert(file);
r = new FileReader(); //本地预览
r.onload = function() {
$_('productEditor').value = r.result;
}
r.readAsDataURL(file); //Base64 */
var form = $("<form method='post'target='_blank'></form>");
form
.attr({
"action" : "${pageContext.request.contextPath}/PreviewProduct"//这里最好加个 path进去 ,如果浏览器直接跳转到 /PreviewNews 前面没有项目名和端口这就尴尬了。
});
var input1 = $("<input type='hidden'>");
input1.attr({
"name" : "brand"
});
input1.val(brand);
form.append(input1);
var input2 = $("<input type='hidden'>");
input2.attr({
"name" : "carmodel"
});
input2.val(carmodel);
form.append(input2);
var input3 = $("<input type='hidden'>");
input3.attr({
"name" : "oenumber"
});
input3.val(oenumber);
form.append(input3);
var input4 = $("<input type='hidden'>");
input4.attr({
"name" : "directionid"
});
input4.val(directionid);
form.append(input4);
var input5 = $("<input type='hidden'>");
input5.attr({
"name" : "content"
});
input5.val(content);
form.append(input5);
var input6 = $("<input type='hidden'>");
input6.attr({
"name" : "directionid"
});
input6.val(directionid);
form.append(input6);
var input7 = $("<input type='hidden'>");
input7.attr({
"name" : "year"
});
input7.val(year);
form.append(input7);
$("html").append(form);
form.submit();
});
});
</script>这是我的HTML
<table>
<tr>
<td class="namestyle">品牌:</td>
<td><input type="text" name="brand" class="input-sm"
value="${p.brand}" style="border: 1px #999 solid;"></td>
</tr>
<tr>
<td class="namestyle">车型:</td>
<td><input type="text" name="carmodel"
value="${p.carmodel}" class="input-sm"
style="border: 1px #999 solid;"></td>
</tr>
<tr>
<td class="namestyle">OE号码:</td>
<td><input type="text" name="oenumber" class="input-sm"
value="${p.oenumber}" style="border: 1px #999 solid;">
</td>
</tr>
<tr>
<td class="namestyle">型号:</td>
<td><input type="text" name="model" class="input-sm"
value="${p.model}" style="border: 1px #999 solid;"></td>
</tr>
<tr>
<td class="namestyle">图片1:</td>
<td><div>
<span class="btn btn-primary fileinput-button"> <span
id="img1">${p.img}</span> <input type="file" name="file"
id="file1" onchange="getFilename1()" value="${p.img}"
accept="image/*" /> </span>
<a id="remove1"><img src="statics/img/remove.png" width="2%"></img></a>
</div></td>
</tr>
<tr>
<td class="namestyle">图片2:</td>
<td><div>
<span class="btn btn-primary fileinput-button"> <span
id="img2">${p.img1}</span> <input type="file" name="file"
id="file2" onchange="getFilename2()" value="${p.img1}"
accept="image/*" /> </span>
<a id="remove2"><img src="statics/img/remove.png" width="2%"></img></a>
</div></td>
</tr>
<tr>
<td class="namestyle">图片3:</td>
<td><div>
<span class="btn btn-primary fileinput-button"> <span
id="img3">${p.img2}</span> <input type="file" name="file"
id="file3" onchange="getFilename3()" value="${p.img2}"
accept="image/*" /> </span>
<a id="remove3"><img src="statics/img/remove.png" width="2%"></img></a>
</div></td>
</tr>
<tr>
<td class="namestyle">图片4:</td>
<td><div>
<span class="btn btn-primary fileinput-button"> <span
id="img4">${p.img3}</span> <input type="file" name="file"
id="file4" onchange="getFilename4()" value="${p.img3}"
accept="image/*" /> </span>
<a id="remove4"><img src="statics/img/remove.png" width="2%"></img></a>
</div></td>
</tr></table>
<div class="text-center">
<button type="button" id="yulan" class="btn btn-warning">预 览</button>
</div>我该如何把它传过去呢 我那段注释的js代码是穿换成base64进制的代码,但是有了这段代码我页面就跳转不过去,报了一个错误:Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.
所以我想请教一下各位 我该怎么把file转换成base64进制并传值到另一个页面
...全文
相关推荐
input type=file 选择图片并且实现预览效果的实例
下面小编就为大家带来一篇input type=file 选择图片并且实现预览效果的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
js获取 input file 图片立即显示
js获取input file控件选择的图片 并且立即在img标签显示出来
input file上传图片预览
input file上传图片预览
input type=file 及时预览图片
type=file选择图片后,在页面预览显示出来
input type=file 显示的浏览 改成英文
input type=file 显示的浏览 改成英文


Web 开发
