asp.net使用plupload实现文件上传

1,页面进行配置,添加pluplaod实现
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="JqueryEasyUiTest.LoginTest.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <!-- Load Queue widget CSS and jQuery -->
<link href="../plupload/jquery.plupload.queue/css/jquery.plupload.queue.css" rel="Stylesheet" />
<script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script>

<!-- Third party script for BrowserPlus runtime (Google Gears included in Gears runtime now) -->
<script type="text/javascript" src="../plupload/plupload.browserplus.js"></script>

<!-- Load plupload and all it's runtimes and finally the jQuery queue widget -->
<script type="text/javascript" src="../plupload/plupload.full.js"></script>
<script type="text/javascript" src="../plupload/jquery.plupload.queue/jquery.plupload.queue.js"></script>

<script type="text/javascript">
    // Convert divs to queue widgets when the DOM is ready
    $(function () {
        $("#uploader").pluploadQueue({
            // General settings
            runtimes: 'gears,flash,silverlight,browserplus,html5',
            url: 'Handler1.ashx',
            max_file_size: '500kb',
            chunk_size: '1mb',
            unique_names: true,

            // Resize images on clientside if we can
            resize: { width: 320, height: 240, quality: 90 },

            // Specify what files to browse for
            filters: [
			{ title: "Image files", extensions: "jpg,gif,png,bmp" }
			
		],

            // Flash settings
            flash_swf_url: '../plupload/plupload.flash.swf',

            // Silverlight settings
            silverlight_xap_url: '../plupload/plupload.silverlight.xap'
        });

        // Client side form validation
        $('form').submit(function (e) {
            var uploader = $('#uploader').pluploadQueue();

            // Files in queue upload them first
            if (uploader.files.length > 0) {
                // When all files are uploaded submit form
                uploader.bind('StateChanged', function () {
                    if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) {
                        $('form')[0].submit();
                    }
                });

                uploader.start();
            } else {
                alert('You must queue at least one file.');
            }

            return false;
        });
    });
</script>
			
</head>
<body>
    <form id="form1" runat="server">
   <div id="uploader">
		<p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p>
	</div>
    </form>
</body>
</html>
2,实现文件上传
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;


namespace JqueryEasyUiTest.LoginTest
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            UploadFile(context);
           
        }
        public void UploadFile(HttpContext context)
        {
        
            context.Response.CacheControl = "no-cache";

         

            string Datedir = DateTime.Now.ToString("yy-MM-dd");

          

            if (context.Request.Files.Count > 0)
            {

                try
                {
                  
                    for (int j = 0; j < context.Request.Files.Count; j++)
                    {

                        HttpPostedFile uploadFile = context.Request.Files[j];

                        if (uploadFile.ContentLength > 0)
                        {

                        
                            string extname = Path.GetExtension(uploadFile.FileName);

                            string fullname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                            string filename = uploadFile.FileName;



                            uploadFile.SaveAs(context.Server.MapPath(@"~/uploads/" + filename));
                          

                        }

                    }

                }

                catch (Exception ex)
                {

                    context.Response.Write(ex.ToString());

                }

            }

        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值