• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
温迪妮的城堡
博客园    首页    新随笔    联系   管理    订阅  订阅

MVC 上传图片

   直接上代码:

     页面:Index.aspx

Code
 1    <form action="/UploadImage/AddNew"  method="post" name="fall" enctype="multipart/form-data">
 2         <table cellspacing="1" cellpadding="5" width="760px" align="center" border="0" bgcolor="#E6E6E6">
 3                <tr>
 4                    <td width="20%" align="right" class="title">
 5                        上传图片:
 6                    </td>
 7                    <td align="left" class="content">
 8                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="jiage">
 9                            <tr>
10                                <td width="36%" rowspan="2" align="center">
11                                    <div id="img_s">
12                                        <img src="/content/images/nopic.gif" border="0" />
13                                    </div>
14                                </td>
15                                <td width="64%" height="41" colspan="2">
16                                    <br />
17                                    <div id="input_s">
18                                        <input name="uploadpic" type="file" id="uploadfile" size="20"/>
19                                      
20                                    </div>
21                                    <br />
22                                    <font color="#999999">图片格式必须为jpg或gif格式,图片大小不超过200K。</font>
23                                </td>
24                            </tr>
25                            <tr>
26                                <td height="30" colspan="2">
27                                    <input type="button" name="BtnUp" value="上传" onclick="UpLoadPic()" />
28                                    <input type="submit" name="submit" value="提交" />
29                                </td>
30                            </tr>
31                        </table>
32                    </td>
33                </tr>
34            </table>
35            <%= ViewData["Error"]  %>
36            
37    </form>

 

 

2. UploadImageController.cs

Code
 1  public ActionResult Index()
 2        {
 3            return View();
 4        }

 5
 6        public ActionResult AddNew()
 7        {
 8            int phWidth = 0;//图片宽度
 9            int phHeight = 0;//图片高度
10            string Pic_Path = "";
11            if (Request.Files.Count > 0)
12            {
13                HttpPostedFileBase file = Request.Files[0];
14                Pic_Path = UpLoadPic(file, out phWidth, out phHeight);
15
16                if (Pic_Path.IndexOf("错误") > -1) //出现错误时,alert错误
17                {
18                    ViewData["Error"] = "<script>alert('" + Pic_Path + "')</script>";
19                }

20            }

21            return RedirectToAction("Index");
22        }

23
24        图片上传#region 图片上传
25        /**//// <summary>
26        /// 图片上传
27        /// </summary>
28        /// <param name="file"></param>
29        /// <returns></returns>

30        public string UpLoadPic(HttpPostedFileBase file, out int phWidth, out int phHeight)
31        {
32            phWidth = 0;
33            phHeight = 0;
34
35            try
36            {
37                string fileName = "";
38                string strDir = System.Configuration.ConfigurationSettings.AppSettings["uploadImage"];
39                if (file.ContentLength > 0)
40                {
41                    if (file.ContentLength < 1024 * 200)
42                    {
43                        string strNew = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName);        //新文件名
44                        fileName = strDir + strNew;
45                        file.SaveAs(Path.Combine(Path.Combine(Request.Path, ""), fileName));
46                        //取高和宽
47                        System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(file.InputStream, true);
48                        phWidth = imgPhoto.Width;
49                        phHeight = imgPhoto.Height;
50                        return strNew;
51                    }

52                    else
53                    {
54                        return "错误:图片大小超过指定大小";
55                    }

56                }

57                else
58                {
59                    return "错误:请上传图片";
60                }

61            }

62            catch (Exception ex)
63            {
64                return "错误:" + ex.Message;
65            }

66        }

67        #endregion

 

3. 脚本

Code
 1//上传图片
 2function UpLoadPic()
 3{
 4    load_img(document.getElementById("uploadfile"),document.getElementById("img_s"))
 5}

 6
 7//图片上传 开始
 8function load_img(src_id,write_id)
 9{
10    var t_html;
11    var p_src="";
12    var valid=0;
13    if (src_id!=null)
14    {
15        p_src=    src_id.value;
16    }

17    if(p_src!='') //上传图片
18    {
19        if(CheckImg(src_id)==0) //符合要求格式
20        {
21            t_html="<img id='ViewImg' name ='ViewImg' src='"+p_src+"' border='0'>";
22            valid+=0;
23        }

24        else //上传图片格式不符合要求
25        {
26            t_html="<img src='/content/images/nopic.gif' border='0'>";
27            valid+=1;
28        }

29    }

30    else //未上传图片
31    {
32        t_html="<img src='/content/images/nopic.gif' border='0'>";
33        alert("请上传图片");
34        valid+=1;
35    }
    
36    write_id.innerHTML=t_html;
37    return valid;
38}

39
40function checkImgSize()
41{
42    var img=new Image();
43    img.src=document.fall.uploadpic.value;
44    alert(img.fileSize);
45}
 
46    
47      
48function CheckImg(src_id)
49{
50    var v = null;
51    var Imgstr = src_id.value;
52    if(Imgstr!="")
53    {
54        if(!(Imgstr.toLowerCase().indexOf(".gif",0)!=-1 || Imgstr.toLowerCase().indexOf(".jpg",0)!=-1 || Imgstr.toLowerCase().indexOf(".jpeg",0)!=-1 ))
55        {
56            alert('上传图片必须为gif、jpeg格式文件');
57            v = 1;
58        }
else
59        {
60            v = 0;
61        }

62    }

63    else
64    {
65        v = 0;
66    }

67    return v;
68}

69//图片上传 结束

 

 

 

 

posted @ 2009-02-06 16:33  温迪妮  阅读(4752)  评论(6)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3