SWFupload 上传控件的使用的使用

-- 首先说个解决 ie兼容的标签

 <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

 

---aspx

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

<!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>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <script src="../SWFupload/swfupload.js" type="text/javascript"></script>
    <script src="../SWFupload/handlers.js" type="text/javascript"></script>
    <script type="text/javascript">
  var swfu;
  window.onload = function () {
   swfu = new SWFUpload({
    // Backend Settings
    upload_url: "/ashx/upload.ashx",//上传处理的文件
                post_params : {
                    "ASPSESSID" : "<%=Session.SessionID %>"//通过post提交的数据
                },

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

    // Event Handler Settings - these functions as defined in 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 : ShowMsg,//上传成功调用的事件
    upload_complete_handler : uploadComplete,

    // Button settings
    button_image_url : "/SWFupload/images/XPButtonNoText_160x22.png",
    button_placeholder_id : "spanButtonPlaceholder",//显示按钮的位置
    button_width: 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 ShowMsg(file, serverData)
  {//serverData:从服务端返回的数据
     var data=serverData.split(":");
     if(data[0]=="ok"){
        document.getElementById("imgok").src=data[1];
     }
  }
    </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>
        <img id="imgok" />
    </div>
    </form>
</body>
</html>

--upload.ashx

 

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;

namespace BookShop.Web.ashx
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class upload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            HttpPostedFile  file = context.Request.Files["Filedata"];//获取上传的文件.
            string fileName = Path.GetFileName(file.FileName); //获取文件名称.
            string fileExtion = Path.GetExtension(file.FileName);//获取扩展名.
            if (fileExtion == ".jpg")
            {
                //file.SaveAs(context.Server.MapPath("/FileUp/" + fileName));
                //context.Response.Write("ok:/FileUp/" + fileName);
                string saveDir = "/FileUp/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
                Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(saveDir)));//建立文件夹

                string fullDir = saveDir + Common.Common.GetStreamMD5(file.InputStream) + fileExtion;//将文件进行MD5运算作为新的文件的名称.
                file.SaveAs(context.Server.MapPath(fullDir));
                context.Response.Write("ok:"+fullDir);


            }
            else
            {
                throw new Exception("类型错误!");
            }
        }

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

你可能感兴趣的:(swfupload)