惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
腾讯CDC
G
Google Developers Blog
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
美团技术团队
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志

博客园 - leegool

常见XSD问题 EntitySpace 常用语句 抗投诉空间 C#用WebClient下载File时操作超时的问题 用C# 实现 Zen Cart 的用户密码加密算法 ASP.NET MVC 3 新特性 ASP.NET MVC 局部缓存实现 用户控件缓存 Partial Output Caching - leegool System.Web.Security.SqlMembershipProvider”要求一个与架构版本“1”兼容的数据库架构。 关于MarshalByRefObject的解释 jQuery Custom Selector JQuery自定义选择器 - leegool 清楚浏览器缓存 Overview of Full Text Stop Words(MSSQL全文索引的干扰词概括)MSSQL 全文索引的最小单词长度 JQuery性能优化 MS SQL 事务隔离级别 认识ASP.NET 中的 AppDomain 国外服务器上 中文成 显示乱码 Sharing cookie Across domain 跨域cookie访问 cookie跨域 web性能优化(最佳) EntitySpaces 2009 Trial Expire 异常
MVC upload image MVC上传图片的例子 - leegool
leegool · 2009-12-13 · via 博客园 - leegool

直接上MVC上传图片的代码了。

View :

代码

<% using (Html.BeginForm(Html.ViewContext.RouteData.Values["action"].ToString(), Html.ViewContext.RouteData.Values["controller"].ToString(), FormMethod.Post, new { enctype = "multipart/form-data" }))
       { 
%>
         
        
<div>
        
<table>
            
<tr>
                
<td>
                    文章图片
                
</td>
                
<td>                    
                  
<input id="txtUploadFile" type="file" name="file" accept="image/gif" onchange="return FilterFileType();"  style="width:350px;" />
                
</td>
                
</tr>
            
<tr>
                
<td>
                
</td>
                
<td>
                
                    
<input type="submit" onclick="$('#IsPublish').attr('value','True');" value="保存并发布" />
                
</td>
            
</tr>
        
</table>
    
</div>
    
        
<script type="text/javascript">
         function FilterFileType() {
            var fullName 
= document.getElementById("txtUploadFile").value;           
            
if (fullName != "")   
              {   
                  var   s,ss;
                  var s 
= fullName;   
                  ss
=   s.substr(s.length-4,s.length);   
                  
if   (ss!=".gif"   &&   ss!=".jpg"   &&   ss!=".bmp")   
                  {   
                      alert(
"图片文件上传只支持.gif   .jpg   .bmp");   
                      
return   (false);   
                  }
              }

         }     

</script>
            
    
<% } %>

Controller:

代码

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(
false)]
        
public ActionResult Create(HttpPostedFileBase file)
        {
            
                
try
                {
                    
if (file != null)
                    {
                        var fileName 
= Path.Combine(Request.MapPath("~/App_Data/Image"), Path.GetFileName(file.FileName));
                        file.SaveAs(fileName);
//保存Img图片
                       }
                 }
                
catch(Exception e)
                {
                    
                }
               
return View();
               
// If we got this far, something failed, redisplay form
            
        }