求助:用jQuery Ajax 上传图片显示“上传失败,错误信息:SyntaxError:语法错误
问题,如标题所述,有时显示“上传失败,错误信息:SyntaxError:拒绝访问”.
代码如下:(HTML页)
<script type="text/javascript" src="../js/singleupload.js"></script>
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../js/jquery.validate.min.js"></script>
<asp:TextBox ID="txtImgUrl" runat="server" CssClass="input w380 left"></asp:TextBox>
<a href="javascript:void(0);" class="files"><input type="file" id="FileUpload" name="FileUpload" /></a>
<span class="uploading">正在上传,请稍候...</span>
(singleupload.js页)
$(function() {
$("#FileUpload").bind("change", function() {
//开始提交
$("#form1").ajaxSubmit({
beforeSubmit: function(formData, jqForm, options){
//隐藏上传按钮
$(".files").hide();
//显示LOADING图片
$(".uploading").show();
},
success: function(data, textStatus) {
if (data.msg == 1) {
$("#txtImgUrl").val(data.msbox);
} else {
alert(data.msbox);
}
$(".files").show();
$(".uploading").hide();
},
error: function(data, status, e) {
alert("上传失败,错误信息:" + e);
$(".files").show();
$(".uploading").hide();
},
url: "../../Tools/SingleUpload.ashx",
type: "post",
dataType: "json",
timeout: 600000
});
});
});
(SingleUpload.ashx 页)
namespace SingleUpload
{
/// <summary>
/// AJAX单文件上传页
/// </summary>
public class SingleUpload : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//检查是否登录后上传操作
if (context.Session["UserId"] == null)
{
context.Response.Write("{msg: 0, msbox: \"请登录后再进行上传文件!\"}");
return;
}
HttpPostedFile _upfile = context.Request.Files["FileUpload"];
string _delfile = context.Request.Params["txtImgUrl"];
if (_upfile == null)
{
context.Response.Write("{msg: 0, msbox: \"请选择要上传文件!\"}");
return;
}
UpLoad upFiles = new UpLoad();
string msg = upFiles.fileSaveAs(_upfile, 0);
//删除已存在的旧文件
if (!string.IsNullOrEmpty(_delfile))
{
string _filename = HttpContext.Current.Server.MapPath(_delfile);
if (File.Exists(_filename))
{
File.Delete(_filename);
}
}