欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

ajaxFileUpload上传文件后提示下载的问题

发布时间:2023/12/15 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 ajaxFileUpload上传文件后提示下载的问题 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在某些版本浏览器下ajaxFileUpload上传文件会提示下载,

1:为什么?

可以观察到,即便返回 JsonResult 在返回的头中也没有任何消息体,直接理解为文本了。

2:解决方案

前端:

function uploadImg(fimgi) {
    if (("#fimg" + fimgi).val().length > 0) {         //alert(("#fimg" + fimgi).val().length > 0) {         //alert(("#fimg" + fimgi).val().length);

    }
    else {
        alert("请选择图片");
        return;
    }
    .ajaxFileUpload({         type: 'post',         url: "/product/UploadProductImage?fimgi=" + fimgi,         secureuri: false,         fileElementId: 'fimg' + fimgi,         dataType: "json",         success: function (data) {             alert("上传成功!");             //alert(data.O);.ajaxFileUpload({         type: 'post',         url: "/product/UploadProductImage?fimgi=" + fimgi,         secureuri: false,         fileElementId: 'fimg' + fimgi,         dataType: "json",         success: function (data) {             alert("上传成功!");             //alert(data.O);("#Img" + fimgi).val(data.O);
        },
        error: function (XMLHttpRequest, textStatus, e) {
            alert(textStatus);
            alert(e);
        }
    });
}

后台改为范围ContentResult,且,ContentType = "text/html"。

public ContentResult UploadProductImage(int fimgi)
{
    HttpPostedFileBase head = this.Request.Files["fimg"+fimgi];

    if (head == null)
    {
        return new ContentResult
        {
            Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "对不起,无上传内容!" }),
            ContentType = "text/html"
        };
    }

    var supportedTypes = new[] { "jpg", "jpeg", "png", "gif", "bmp" };
    var fileExt = System.IO.Path.GetExtension(head.FileName).Substring(1);
    if (!supportedTypes.Contains(fileExt))
    {
        return new ContentResult
        {
            Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "对不起,只能上传 jpg, jpeg, png, gif, bmp!" }),
            ContentType = "text/html"
        };
    }

    if (head.ContentLength > 1024 * 1024)
    {
        return new ContentResult
        {
            Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "对不起,大小超出限制1M!" }),
            ContentType = "text/html"
        };
    }

    var r = new Random();
    var filename = Guid.NewGuid().ToString("N") + "." + fileExt;
    string path = this.Server.MapPath("~/upload/product");

    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }

    var filepath = Path.Combine(path, filename);
    head.SaveAs(filepath);
    string webPath = "/upload/product/" + filename;

    return new ContentResult
    {
        Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R()
        {
            F = 0,
            M = "上传成功,保存为:" + webPath + "!",
            O = webPath
        }),
        ContentType = "text/html"
    };
}


本文转自最课程陆敏技博客园博客,原文链接:http://www.cnblogs.com/luminji/p/4677103.html,如需转载请自行联系原作者

总结

以上是生活随笔为你收集整理的ajaxFileUpload上传文件后提示下载的问题的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。