• ASP.NET FileUpload上传图片方法并解决上传文件大小 TC


    在web.config配置

    <system.web>

        <httpRuntime executionTimeout="300" maxRequestLength="204800" useFullyQualifiedRedirectUrl="false"/> 

    </system.web> 

     C#

     if (FuPhoto.HasFile)

                {

                    string fileContentType = FuPhoto.PostedFile.ContentType;

                    if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")

                    {

                        string name = FuPhoto.PostedFile.FileName;                  // 客户端文件路径


                        FileInfo file = new FileInfo(name);

                        // 缩略图文件名称

                        string fileName_s = DateTime.Now.ToString("yyyyMMddHHmmssS") + file.Name;

                        // 服务器端文件路径

                        string webFilePath_s = Server.MapPath("../UploadBlogPhoto/" + userID + "/" + fileName_s);

                        string strCreatPath = "../UploadBlogPhoto/" + userID;


                        if (!File.Exists(webFilePath_s))

                        {

                            try

                            {

                                if (FuPhoto.PostedFile.ContentLength > 2048000)

                                {

                                    Jscript.Alert("提示:上传图片大小不能超过2048K");

                                    // Response.Write("<script language='JavaScript'>alert('上传图片大小不能超过2048K'):history.go(-1);</script>");

                                    return;

                                }

                                if (Directory.Exists(Server.MapPath(strCreatPath)) == false)//如果不存在就创建file文件夹

                                {

                                    Directory.CreateDirectory(Server.MapPath(strCreatPath));

                                }

                                FuPhoto.SaveAs(webFilePath_s);  // 使用 SaveAs 方法保存文件

              //model PiUrl属性字段 

                                bumodel.PicUrl = "/UploadBlogPhoto/" + userID + "/" + fileName_s;//压缩过的图片

              //保存方法

                                buBll.Add(bumodel);//添加相片


                                Response.Write("<script language='JavaScript'>alert('添加相片成功');window.location='BlogUserPic.aspx?userID=" + userID + "';</script>");

                                Response.End();

                            }

                            catch (Exception ex)

                            {

                                Jscript.Alert("提示:相片上传失败,失败原因:" + ex.Message);

                            }

                        }

                        else

                        {

                            Jscript.Alert("提示:相片已经存在,请重命名后上传");

                        }

                    }

                    else

                    {

                        Jscript.Alert("提示:相片类型不符");

                    }

                }


      

    作者:Mr S.R Lee
    出处:http://www.cnblogs.com/LeeYongze
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

  • 相关阅读:
    ES 分词器设置+同义词设置
    ES 重写分数查询
    ReadWriteLock源码阅读
    java编程之Lambda、Stream、Optional
    AQS源码阅读
    MinIOGithub标星19K+Star,10分钟自建对象存储服务!
    使用Jenkins一键打包部署SpringBoot应用,就是这么6!
    网络与系统攻防技术|实验四 恶意代码分析实践
    React的通信方式与状态管理:Redux与Mobx
    React新旧版本生命周期对比
  • 原文地址:https://www.cnblogs.com/LeeYongze/p/1744640.html
Copyright © 2020-2023  润新知