(一) asp.net WebUploader 上传

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

1、web.config 代码:


<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代码:


<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('
'" class="item">' + '

' + file.name + '

'
+ '

等待上传...

'
+ '
'
); }); 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();
    }

    /// 
    /// 文件保存操作
    /// 
    /// 
    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 WebUploader 上传)