ajaxFileUpload上传实例代码教程

//上传图标

        function ajaxFileUpload() {

            $.ajaxFileUpload({

                url: "@Url.Action("UploadImgFile", "Upload")"//用于文件上传的服务器端请求地址

                secureuri: false//一般设置为false

                fileElementId: $("input[name=file_upload]"), //文件上传空间的id属性

                dataType: "text"//返回值类型

                success: function (data, status) {  //服务器成功响应处理函数

                    var data2 = JSON.parse(data);

                    if (data2.ResultType == "@OperationResultType.Success.ToInt32()") {

                        $("input[name=Icon]").val(data2.ResultMessage);

                        $("label.img-label").text("重传");

                    }

                    else {

                        showMessage(data2.ResultMessage);

                    }

                },

                error: function (data, status, e) {//服务器响应失败处理函数

                    showMessage("服务器未响应");

                }

            });

            return false;

        }

   /// <summary>

        /// 浏览上传文件

        /// </summary>

        /// <param name="file_upload">文件</param>

        /// <returns></returns>

        public ContentResult UploadImgFile(HttpPostedFileBase file_upload)

        {

            OperationResult result;

            if (file_upload == null)

            {

                result = new OperationResult(OperationResultType.Error, "请选择要上传的图片");

            }

            else

            {

                var fileExtension = Path.GetExtension(file_upload.FileName).ToLower();

                if (!WebConfigInfo.ImageSupportType.Contains(fileExtension))

                {

                    result = new OperationResult(OperationResultType.Error, "请上传" + WebConfigInfo.ImageSupportType+"格式的图片");

                }

                else if (file_upload.ContentLength > Convert.ToInt32(WebConfigInfo.IconFileSizeLimit.Remove(WebConfigInfo.IconFileSizeLimit.LastIndexOf(' '))) * 1024)

                {

                    result = new OperationResult(OperationResultType.Error, "请上传最大不超过" + WebConfigInfo.IconFileSizeLimit+"的图片");

                }

                else

                {

                    string path = "/upload/" + DateTime.Now.ToString("yyyyMMdd");

                    string uploadDir = Server.MapPath(WebConfigInfo.VirtualDirectoryName + path);

                    // 如果上传课程文件夹不存在,则创建

                    if (!Directory.Exists(uploadDir))

                        Directory.CreateDirectory(uploadDir);

                    string fileUrl = string.Format("{0}/{1}{2}", path, Guid.NewGuid().ToString().Replace("-", string.Empty), fileExtension);

                    file_upload.SaveAs(Server.MapPath(WebConfigInfo.VirtualDirectoryName + fileUrl));

                    result = new OperationResult(OperationResultType.Success, fileUrl);

                }

            }

            var json = string.Format("{0}\"ResultType\":\"{1}\",\"ResultMessage\":\"{2}\"{3}""{", result.ResultType.ToInt32(), result.Message, "}");

            return Content(json);

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执刀人的工具库

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值