步步为营-86-WSFUpload组件

文件上传组件,所需js文件和图片在百度网盘对应的文件夹下

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SWFUpload_Demo.aspx.cs" Inherits="BookShop.Web.Test.SWFUpload_Demo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../../js/jquery-1.7.1.js"></script>
    <script src="../handlers.js"></script>
    <script src="../swfupload.js"></script>
    <script type="text/javascript">
        var swfu;
        window.onload = function () {
            swfu = new SWFUpload({
                //指定要上传的路径
                upload_url: "/ashx/FileUpload.ashx",
                post_params: {
                    "ASPSESSID": "<%=Session.SessionID %>"
                },

                // File Upload Settings
                file_size_limit: "2 MB",
                file_types: "*.jpg;*.gif",
                file_types_description: "JPG Images",
                file_upload_limit: 0,    // Zero means unlimited

                // 事件处理了的三个主要方法定义在 Handlers.js
                //  The handlers are not part of SWFUpload but are part of my website and control how
                //  my website reacts to the SWFUpload events.
                swfupload_preload_handler: preLoad,
                swfupload_load_failed_handler: loadFailed,
                file_queue_error_handler: fileQueueError,
                file_dialog_complete_handler: fileDialogComplete,
                upload_progress_handler: uploadProgress,
                upload_error_handler: uploadError,
                upload_success_handler: showImage,
                upload_complete_handler: uploadComplete,

                // 按钮设置
                button_image_url: "/SWFUpload/images/XPButtonNoText_160x22.png",
                button_placeholder_id: "spanButtonPlaceholder",
                button_ 160,
                button_height: 22,
                button_text: '<span class="button">请选择上传图片<span class="buttonSmall">(2 MB Max)</span></span>',
                button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 14pt; } .buttonSmall { font-size: 10pt; }',
                button_text_top_padding: 1,
                button_text_left_padding: 5,

                // Flash Settings
                flash_url: "/SWFUpload/swfupload.swf",    // Relative to this file
                flash9_url: "/SWFUpload/swfupload_FP9.swf",    // Relative to this file

                custom_settings: {
                    upload_target: "divFileProgressContainer"
                },

                // Debug Settings
                debug: false
            });
        }
        //上传成功以后调用该方法
        function showImage(file, serverData) {
            $("#showPhoto").attr("src", serverData);
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
 
    <div id="content">
        <div id="swfu_container" style="margin: 0px 10px;">
            <div>
                
                <span id="spanButtonPlaceholder"></span>
               
            </div>
             
            <div id="divFileProgressContainer" style="height: 75px;"></div>
            
            <div id="thumbnails"></div>
            <img id="showPhoto"></img>
        </div>
        </div>
    </form>
</body>
</html>
前台代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace BookShopManager.Web.Ashx
{
    /// <summary>
    /// FileUpload 的摘要说明
    /// </summary>
    public class FileUpload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpPostedFile file = context.Request.Files["Filedata"];
            if (file != null)
            {
                string fileName = Path.GetFileName(file.FileName);
                string fileExt = Path.GetExtension(fileName);
                if (fileExt == ".jpg")
                {
                    string dir = "/FileUpload/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "/";
                    if (!Directory.Exists(context.Request.MapPath(dir)))
                    {
                        Directory.CreateDirectory(context.Request.MapPath(dir));
                    }
                    string newFileName = Guid.NewGuid().ToString();
                    string fullDir = dir + newFileName + fileExt;
                    file.SaveAs(context.Request.MapPath(fullDir));
                    context.Response.Write(fullDir);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
服务器端代码
原文地址:https://www.cnblogs.com/YK2012/p/7451308.html