积极答复者
jquery.form 异步上传图片,IE下拒绝访问,chrome正常

问题
-
我使用了jquery.form.js来异步提交表单上传图片,然后我把包含<input type="file" name="txtfile" id="txtfile" />的form给隐藏了,让用户通过点击一个<input type="button" value="选择图片" />来触发txtfile的click事件,从而选择图片。当选择了图片后,在txtfile的change事件里,提交表单上传图片,返回图片地址。
这个过程在chrome没问题,但是在IE里,txtfile的change事件提交上传图片时,弹出“拒绝访问”。请教各位大侠这是怎么回事?有没有其他解决途径,拜谢!
<head> <script src="http://res...../js/jquery-1.8.2.min.js"></script> <script src="http://res...../js/jquery.form.js"></script> <title>传图片</title> <script type="text/javascript"> $(function () { (function () { //模拟一个选择文件的按键,选择完图片后就通过Form上传 $("#txtFile").change(function () { var _val = $(this).val(); if (_val.length > 4) { var _ext = _val.substr(_val.length - 3, 3); if (_ext == "jpg" || _ext == "png" || _ext == "gif") { $("#frmUploadImg").ajaxSubmit({ //这里在IE下会涉及权限问题 url: 'ashx/img.ashx', success: function (data) { $("#imgPreview").attr("src", data); }, type: 'post' }); } else { alert('请选择格式为jpg、png或gif的图片!'); } } }); $("#btnFile").click(function () { $("#txtFile").click();//触发选择“浏览”按钮,IE下权限不够 }); })(); }); </script> </head> <body> <div class="uploadpic"> <div class="uploadpic-hd"> <h1>传图片<span>图说八道,很邪恶,很有趣</span></h1> </div> <div class="uploadpic-bd"> <div class="uploadpic-bd-file"> <input type="button" value="选择文件" id="btnFile" /> <div style="display:none"> <form id="frmUploadImg" enctype="multipart/form-data"> <input type="file" name="img" onkeypress="return false;" id="txtFile" /> </form> </div> </div> <div class="uploadpic-bd-preview"> <dl> <dt> <img id="imgPreview" /> </dt> <dd> <p> <input type="text" />请输入标题 </p> <p> <select> <option>默认专辑</option> </select>请选择专辑 </p> <p> <textarea placeholder="说点什么吧,让分享更精彩~~"></textarea> <label class="counter"><em>0</em> / 140</label> <label class="overlength">您的描述超过了140字</label> </p> <p><button type="submit">发布</button></p> </dd> </dl> </div> </div> </div> </body>
public class img : IHttpHandler { public void ProcessRequest (HttpContext context) { //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); //路径保存在webconfig HttpPostedFile img = context.Request.Files[0]; string strSaveFileName = context.Server.MapPath("/upload/images/" + img.FileName); img.SaveAs(strSaveFileName); context.Response.Write("/upload/images/" + img.FileName); } public bool IsReusable { get { return false; } } }
以上是所有代码了,这个问题我在 http://social.microsoft.com/Forums/zh-CN/5b8a0a76-6361-45fb-bbdd-c71ad19ce840/jqueryform-iechrome?forum=295 提问过了,可是现在好像被锁定了,无法操作,所以我只能重新提问一次,敬请谅解
开心了就笑,不开心了就过会儿再笑
- 已编辑 NewJoin 2013年11月21日 5:15
答案
全部回复
-
Hi,
You can try adding the content type meta as the first item in your head tag:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="/ietest/jquery.js"></script>
<script type="text/javascript" src="/ietest/test.js"></script>
</head>
For more information, you can refer here
http://stackoverflow.com/questions/2960153/permission-denied-with-internet-explorer-and-jquery
http://stackoverflow.com/questions/1220481/ajax-image-upload-form-returns-forbidden-403-error
Hope it can help you.We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.