ASP.Net MVC 3中如何上传图片

图片上传的插件很多,但很多时候还是不能切合我们的需求,我这里给大家分享个我用一个form,file实现上传四张图片的小demo。完全是用jquery前后交互,没有用插件。

最终效果图如下:

玩过花田人可能有些眼熟,原型就是来自于花田网中的图片上传。

引用的脚本有:

<script src="../../Scripts/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="../../Scripts/jquery-1.8.3.js"></script>
  <link href="../../Content/bootstrap/css/bootstrap.min.css" rel="stylesheet"  />
  <script src="../../Content/bootstrap/js/bootstrap.min.js"></script>  <script src="../../Content/JS/form.js"></script>

基本思路:用户点击相关div触发file元素打开浏览框----》用setInterval不断检测file文件是否选中文件---》选中之后触发form自动提交到后台---》后台将文件存储到临时文件夹返回图片在临时文件夹的地址---》窗口提示上传功能并预览图片---》用户点击发布---》后台保存图片(移动到指定文件夹,并存入数据库)

整个过程form和file都是隐藏的。另外的效果

1. 开始的时候只显示一个上传框,前一个上传完成后,显示下一个上传框。并总是显示在最后面。

2. 有图片的时候才能提交和输入描述。

3.上传成功之后,删除后才能再次上传。

Html+css

.uploadbox {
            float: left;width: 115px;height: 115px;
            background-color: #eeeeee;
            margin-right: 10px;color: white;text-align: center;cursor: pointer;
            position: relative;display: none;
        }
        .uploadbox:hover {
              background-color: #b4b4b4;
        }
       .stt{ font-size: xx-small;display: block;}
       .add{  vertical-align: baseline;display: block;height: auto;font-size: 8em; height: 40px;padding: 10px;margin-top: 30px}
      .imgcontainer{ width: 100%;height: 100%}  .imgcontainer img{ max-height: 115px;max-width: 115px;}
        .inputdiv{ clear: both;display: block;padding-top: 10px;}
        #Remark{ width:478px;}
       .infospan{display: none; height: 20px;position: absolute;bottom: 0;right: 0;width: 100%;background-color: #00bfff;z-index: 1;clear: both;opacity: 0.8;}
       .closespan{display: none; position: absolute;right: -7px;top: -7px;width: 14px;height: 14px;border-radius: 7px;background-color: white;border: 1px gainsboro solid;color: gray;
           font-weight: bold;font-size: medium;
           line-height: 16px;
       }   .closespan:hover{ color: red;}
       #imgupload form{ display: none;}
       .imguploadmessage{ font-size: small;color: green;float: left;margin-left: 10px;}
       .carclose{ cursor: pointer;
           background-color: white;opacity: 1;
           position: absolute;z-index: 99;top:0px;right: -40px;
           width: 40px;height:40px;border-radius: 20px;text-align: center;
           line-height: 40px;font-size: 2em;color: gray;
       }.carclose:hover{ color: green;background-color: #eeeeee}
<div id="imgupload" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4>上传图片</h4>
        </div>
        <div class="modal-body">
            <div class="uploadbox" data-count="0" style="display: inline;">
                <span class="closespan" title="删除照片">&times;</span>
                <div class="imgcontainer">
                    <div class="add">+</div>
                    <div class="stt">点击上传</div>
                </div>
                <span class="infospan">
                    <img src='../../Content/Photos/loading.gif'  />正在上传...</span>
            </div>
            <div class="uploadbox" data-count="0"  >
                <span class="closespan" title="删除照片">&times;</span>
                <div class="imgcontainer">
                    <div class="add">+</div>
                    <div class="stt">点击上传</div>
                </div>
                <span class="infospan"> <img src='../../Content/Photos/loading.gif'  />正在上传...</span>
            </div>
            <div class="uploadbox"data-count="0" >
                <span class="closespan" title="删除照片">&times;</span>
                <div class="imgcontainer">
                    <div class="add">+</div>
                    <div class="stt">点击上传</div>
                </div>
                <span class="infospan"> <img src='../../Content/Photos/loading.gif'  />正在上传...</span>
            </div>
            <div class="uploadbox"data-count="0"  >
                <span class="closespan" title="删除照片">&times;</span>
                <div class="imgcontainer">
                    <div class="add">+</div>
                    <div class="stt">点击上传</div>
                </div>
                <span class="infospan"> <img src='../../Content/Photos/loading.gif'  />正在上传...</span>
            </div>
            <div class="inputdiv"><input type="text" disabled="disabled" id="Remark" name="Remark" placeholder="补充说明下~" /></div>
        </div>
            <div class="modal-footer">
             上传大小在8k-10M之间 <span class="imguploadmessage"></span>
              <button class="btn btn-success "  disabled="disabled"   id="imgsubmit">发布</button>
              <form action="/User/UpLoadPhoto" method="POST" enctype="multipart/form-data" name="ImgForm" id="ImgForm">
              <input type="file" name="file" id="imgFlie"  required="required"  />
              <input type="submit" name="subt" value="上传图片"  />
              </form>
            </div>

    </div>

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索上传图片文件
, 图片
, display
, 上传图片
, 图片上传
, color
, px
, stt mram
, imgupload
, height
, 后台上传图片
, MVC图片上传
, MVC图片预览
font-size
,以便于您获取更多的相关知识。

时间: 2024-08-24 02:47:43

ASP.Net MVC 3中如何上传图片的相关文章

asp.net MVC项目中 没有easyui插件的智能提示

问题描述 asp.net MVC项目中 没有easyui插件的智能提示 5C vs2015 中的mvc4项目,已经分别把jQuery.min.js jquery.easyui.min.jseasyui-lang-zh_CN.js依次拖入,写js代码的时候 智能提示只有jquery中的属性和方法 没有easyui的(例如$.messager就出不来智能提示),但是可以正常执行出来插件效果,尝试编辑cshtml.html格式的文件都不提示,尝试把 jquery.easyui.min.js的文件名改为

浅谈ASP.NET MVC 3中如何使用Model

昨天博客发了新文章,讲一下我对如何使用MVC中的Model的看法,不是什么大技术,当是一个技术讨论^^ 原文地址:http://www.youguanbumen.net/Article.aspx?id=79 原文: 前两天写了个文章ASP.NET MVC 3 -- Model远程验证,主要记录了一下ASP.NET MVC 3中新增的RemoteAttribute类的使用,得益于这个类,我们可以在模型中为属性配置客户端远程校验的业务,文章中给了出一个简单的实体类MyUser_Add,举了一个最常见

领先技术:在ASP.NET MVC 4中创建为移动设备优化的视图

如果深入探讨有关编写移动设备网站的常识性考虑因素,会发现其中有一种内在矛盾.一方面,客户在其 编写应用程序和网站的方法中强烈要求(或乐于要求)移动优先.另一方面,同一些人又经常称赞 CSS 媒体 查询和流体布局.我所发现的矛盾在于经常利用 CSS 媒体查询和流体布局并未在其他内容之前优先处理移动 方面,它不是一种移动优先的方法.在本文中,我将介绍如何使用服务器端逻辑为给定设备呈现最佳的显示效 果,并介绍 ASP.NET MVC 4 的一种新功能,称为显示模式. 问题不在于 CSS 媒体查询作为一

使用ASP.NET MVC Futures中的异步Action

之前看过老赵这两篇文章.也研究了一下老赵异步的实现方式. 不过感觉自己扩展的话,在ASP.NET MVC中使用异步还真是麻烦,刚好看到从RC1版开始 ASP.NET MVC Futures中提供了几个支持异步的类. 相关的类包括AsyncActionDescriptor.AsyncController.AsyncControllerActionInvoker. AsyncManager.AsyncResultWrapper.AsyncTimeoutAttribute.NoAsyncTimeout

在ASP.NET MVC 4中使用Kendo UI Grid

Kendo UI 是Telerik推出的一套based on jQuery 的 Framework,提供了很多控件(Menu .Grid .Combox等...), 底层以Html5 + jQuery 来打造,并且兼容于各大浏览器,包含IE7.IE8.相关介绍可以参考AJAX式数据清单的新选择-Kendo UI Grid. 以下内容参考台湾的黑老大的文章:在ASP.NET MVC 4中使用Kendo UI Grid 建立一个ASP.NET MVC 4专案 使用NuGet安装KendoUIWeb及

ASP.NET MVC 3中301永久重定向不带www域名到带www的域名

使用 ASP.NET 又喜欢跟进新技术的朋友可能已经知道,在 ASP.NET 4.0 中增加了 Response.RedirectPermanent() 方法来实现永久重定向,方法的作用在注释中解释的很清楚:执行从所请求 URL 到所指定 URL 的永久重定向,并提供用于完成响应的选项. 在 ASP.NET MVC 3 项目中,我们可以根据需要来使用以下写法实现永久重定向:  代码如下 复制代码 public ActionResult Index() {     return RedirectP

【初学者指南】在ASP.NET MVC 5中创建GridView

介绍 在这篇文章中,我们将会学习如何在 ASP.NET MVC 中创建一个 gridview,就像 ASP.NET Web 表单中的 gridview 一样.服务器端和客户端有许多可用的第三方库,这些库能够提供所有必需的功能,如 Web 表格中的搜索.排序和分页等.是否包含这些功能,取决于应用的特殊需求,例如在客户端和服务器端提供搜索或其它功能的需求等. 可用的库 以下是一些可用的库和插件: Grid.Mvc MVCGrid.NET PagedList.MVC JQuery.Grid JQuer

使用ASP.NET MVC Futures 中的异步Action

  之前看过老赵这两篇文章.也研究了一下老赵异步的实现方式. 不过感觉自己扩展的话,在ASP.NET MVC中使用异步还真是麻烦,刚好看到从RC1版开始 ASP.NET MVC Futures中提供了几个支持异步的类. 相关的类包括AsyncActionDescriptor.AsyncController.AsyncControllerActionInvoker.AsyncManager.AsyncResultWrapper.AsyncTimeoutAttribute.NoAsyncTimeou

Asp.net mvc 2中使用Ajax的三种方式

在Asp.net MVC中,我们能非常方便的使用Ajax.这篇文章将介绍三种Ajax使用的方式,分别为原始的Ajax调用.Jquery.Ajax Helper.分别采用这三种方式结合asp.net mvc去实现一个史上最简单的留言板. 首先看一下原始的Ajax的调用的 定义CommentController,代码如下: public class CommentController : Controller { private IList<string> _comments = new List