(一) asp.net WebUploader 上传

系统环境:windows 7
应用服务器:IIS7.0
编译器:visual Studio 2013
webuploader 下载地址:http://fex.baidu.com/webuploader/download.html
网络环境:本机或局域网
测试结果:几十M 文件一般问题不大。

1、web.config 代码:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime maxRequestLength="2147483647" appRequestQueueLimit="1200" executionTimeout="1200"/>
 </system.web>
  <system.webServer>
    <security>
      <requestFiltering >
        <requestLimits maxAllowedContentLength="2147483647" ></requestLimits>
      </requestFiltering>
    </security>

  </system.webServer>
</configuration>

2、新建html(example.html),html代码:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <link href="dist/webuploader.css" rel="stylesheet" />
    <script src="dist/jquery.js"></script>
    <script src="dist/webuploader.js"></script>
</head>
<body>
    <div id="uploader" class="wu-example">
        <!--用来存放文件信息-->
        <div id="thelist" class="uploader-list"></div>
        <div class="btns">
            <div id="picker">选择文件</div>
            <button id="ctlBtn" class="btn btn-default">开始上传</button>
        </div>
    </div>

    <script>

        var uploader = WebUploader.create({

            // swf文件路径
            swf:  '/dist/Uploader.swf',

            // 文件接收服务端。
            server: 'fileupload.ashx',

            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建,可能是input元素,也可能是flash.
            pick: '#picker',

            // 不压缩image, 默认如果是jpeg,文件上传前会压缩一把再上传!
            resize: false
        });

        // 当有文件被添加进队列的时候
        uploader.on('fileQueued', function (file) {
            //alert(123);
            $("#thelist").append('<div id="' + file.id + '" class="item">' +
                '<h4 class="info">' + file.name + '</h4>' +
                '<p class="state">等待上传...</p>' +
            '</div>');
        });

        uploader.on('uploadSuccess', function (file) {
            $('#' + file.id).find('p.state').text('已上传');
        });

        uploader.on('uploadError', function (file) {
            $('#' + file.id).find('p.state').text('上传出错');
        });

        uploader.on('uploadComplete', function (file) {
            $('#' + file.id).find('.progress').fadeOut();
        });

        $("#ctlBtn").on('click', function () {
            if ($(this).hasClass('disabled')) {
                return false;
            }
            uploader.upload();

        });
    </script>
</body>
</html>

3、新建一般处理程序(),代码如下:

<%@ WebHandler Language="C#" Class="fileupload" %>

using System;
using System.Web;
using System.Text;
using System.IO;

public class fileupload : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.ContentEncoding = Encoding.UTF8;
        if (context.Request["REQUEST_METHOD"] == "OPTIONS")
        {
            context.Response.End();
        }
        SaveFile();
    }

    /// <summary>
    /// 文件保存操作
    /// </summary>
    /// <param name="basePath"></param>
    private void SaveFile(string basePath = "~/Upload/Images/")
    {
        var name = string.Empty;
        basePath = (basePath.IndexOf("~") > -1) ? System.Web.HttpContext.Current.Server.MapPath(basePath) :
        basePath;
        HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

        if (!Directory.Exists(basePath))
            Directory.CreateDirectory(basePath);

        var suffix = files[0].ContentType.Split('/');
        var _suffix = suffix[1].Equals("jpeg", StringComparison.CurrentCultureIgnoreCase) ? "" : suffix[1];
        var _temp = System.Web.HttpContext.Current.Request["name"];

        if (!string.IsNullOrEmpty(_temp))
        {
            name = _temp;
        }
        else
        {
            Random rand = new Random(24 * (int)DateTime.Now.Ticks);
            name = rand.Next() + "." + _suffix;
        }

        var full = basePath + name;
        files[0].SaveAs(full);
        var _result = "{\"jsonrpc\" : \"2.0\", \"result\" : null, \"id\" : \"" + name + "\"}";
        System.Web.HttpContext.Current.Response.Write(_result);
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
ASP.NET MVC WebUploader 是一个基于 ASP.NET MVC 框架的文件上传插件。 首先,ASP.NET MVC 是一种基于模型-视图-控制器(MVC)的框架,适用于构建 Web 应用程序。它提供了一种分离关注点的架构模式,使开发人员能够更好地组织和管理代码。 WebUploader 是一个 JavaScript 插件,可以实现在 Web 页面中进行文件上传的功能。它具有以下特点: 1. 多文件上传WebUploader 允许用户一次选择并上传多个文件,大大提高了效率。 2. 断点上传:如果上传过程中出现网络中断或其他问题,WebUploader 支持断点续传功能,能够从断点处恢复上传,减少数据的丢失。 3. 异步上传WebUploader 使用异步上传方式,即文件在上传过程中不会阻塞用户的其他操作。 4. 文件验证:WebUploader 可以对文件类型、大小等进行验证,确保上传的文件符合预期。 基于 ASP.NET MVC 框架,使用 WebUploader 可以实现以下步骤: 1. 在项目中引入 WebUploader 插件的相关文件,包括 JavaScript 和 CSS 文件。 2. 在 View 视图文件中添加文件上传的 HTML 元素,用于显示文件上传按钮和进度条等界面。 3. 在 Controller 控制器中编写处理文件上传的代码逻辑,包括验证上传文件的类型和大小等,并将文件保存到服务器指定的位置。 4. 在 View 视图文件中使用 JavaScript 脚本,与服务器进行交互,包括初始化 WebUploader、处理上传过程和结果等。 通过以上步骤,可以实现在 ASP.NET MVC 项目中使用 WebUploader 插件进行文件上传的功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值