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

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园_首页
H
Help Net Security
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
GbyAI
GbyAI
Scott Helme
Scott Helme
D
Docker
Hacker News: Ask HN
Hacker News: Ask HN
P
Privacy & Cybersecurity Law Blog
Jina AI
Jina AI
雷峰网
雷峰网
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
G
GRAHAM CLULEY
C
Cisco Blogs
The Hacker News
The Hacker News
F
Full Disclosure
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
G
Google Developers Blog
量子位
K
Kaspersky official blog
Cisco Talos Blog
Cisco Talos Blog
The Cloudflare Blog
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
Microsoft Security Blog
Microsoft Security Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
P
Palo Alto Networks Blog
H
Heimdal Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Schneier on Security
Schneier on Security
The Register - Security
The Register - Security
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Blog of Author Tim Ferriss
N
News and Events Feed by Topic
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
V
V2EX
爱范儿
爱范儿

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