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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
Y
Y Combinator Blog
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
S
SegmentFault 最新的问题
J
Java Code Geeks
V
Visual Studio Blog
H
Help Net Security
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - Franky
B
Blog RSS Feed
The Cloudflare Blog
MyScale Blog
MyScale Blog
月光博客
月光博客
Microsoft Security Blog
Microsoft Security Blog
美团技术团队

博客园 - 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
            
        }