swfupload实现多文件上传,多文件选择(asp.net版本)

swfupload可以实现一次选择多个文件,并且上传!可以上传缩略图,也可以上传原图!

现在就给大家分享一下吧!

1.从官网上下载asp.net程序包

2.修改部分文件,适合自己!总体思路是:

1.修改类Thumbnail,添加缩略图成员

2.修改上传控件,创建内存流存储缩略图数据流

3.上传保存,从Thumbnail类中读取数据流,使用文件流保存图片

    并保存到数据库中。

网易的博客好像不能添加附件啊!那我就给大家展示代码吧!如果谁需要源码给我留个邮箱,我会一一发送的!

default前台

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>多文件上传工具</title>
 <link href="../css/default.css" rel="stylesheet" type="text/css" />
 <script type="text/javascript" src="../swfupload/swfupload.js"></script>
 <script type="text/javascript" src="js/handlers.js"></script>
 <script type="text/javascript">
  var swfu;
  window.onload = function () {
   swfu = new SWFUpload({
    // 后端设置
    upload_url: "upload.aspx",
                post_params : {
                    "ASPSESSID" : "<%=Session.SessionID %>"
                },

    // 上传文件设置
    file_size_limit : "2 MB",
    file_types : "*.jpg",
    file_types_description : "JPG Images",
    file_upload_limit : 0,    
    

    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 : uploadSuccess,
    upload_complete_handler : uploadComplete,

    // 浏览图片按钮设置
    button_image_url : "images/select.png", //图片路径
    button_placeholder_id : "spanButtonPlaceholder", //所要加载的控件id
    button_width: 48,
    button_height: 22,
    button_text : '<span class="button">选  择</span>',
    button_text_style: '.button { font-family: Helvetica, Arial, sans-serif; font-size: 13px; } .buttonSmall { font-size: 10pt; }',


    button_text_top_padding: 1,
    button_text_left_padding: 5,

    // Flash Settings
    flash_url: "../swfupload/swfupload.swf", 
    flash9_url : "../swfupload/swfupload_FP9.swf", 

    custom_settings : {
     upload_target : "divFileProgressContainer"//所选择图片呈现的位置id
    },

    // Debug Settings
    debug: false
   });
  }
 </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>   
              
                <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="上 传"   /> </br>  
                   
                <asp:Button ID="btnClear" runat="server" Text="删 除" OnClick="btnClear_Click"  />
                    
             </div>   
            <div id="divFileProgressContainer" style="height: 75px;"></div>   
            <div id="thumbnails"></div>   
        </div>   
    </div>  

    </form>

default后台

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session.Clear();
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Session["file_info"] != null)
        {
            List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;

            string UploadPath = Server.MapPath("upload/");

            foreach (Thumbnail img in thumbnails)
            {
                FileStream fs = new FileStream(UploadPath + img.ID + ".jpg", FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(img.miniData);//上传原图
               // bw.Write(img.Data);//上传缩略图
                bw.Close();
                fs.Close();
            }
            Response.Write("<script>alert('图片上传成功!');</script>");

            Session.Clear();
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        Session.Clear();
    }


}
upload.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
  
  System.Drawing.Image thumbnail_image = null;
  System.Drawing.Image original_image = null;
  System.Drawing.Bitmap final_image = null;
  System.Drawing.Graphics graphic = null;
  MemoryStream ms = null;//缩略图信息流
        MemoryStream maxms = null;//原图信息流

  try
  {
            // 获取数据

            HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];

            // 上传的图像检索

            original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream);
            int width = original_image.Width;
            int height = original_image.Height;

            // 计算新的宽度和高度

            int target_width = 100;
            int target_height = 100;
            int new_width, new_height;

            float target_ratio = (float)target_width / (float)target_height;
            float image_ratio = (float)width / (float)height;

            if (target_ratio > image_ratio)
            {
                new_height = target_height;
                new_width = (int)Math.Floor(image_ratio * (float)target_height);
            }
            else
            {
                new_height = (int)Math.Floor((float)target_width / image_ratio);
                new_width = target_width;
            }

            new_width = new_width > target_width ? target_width : new_width;
            new_height = new_height > target_height ? target_height : new_height;

            //创建缩略图
            final_image = new System.Drawing.Bitmap(target_width, target_height);
            graphic = System.Drawing.Graphics.FromImage(final_image);
            graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.Rectangle(0, 0, target_width, target_height));
            int paste_x = (target_width - new_width) / 2;
            int paste_y = (target_height - new_height) / 2;
            graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; /* new way */

            graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height);

            // 存储缩略图数据流
            ms = new MemoryStream();
           final_image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            
           // 存储原图数据流
           maxms = new MemoryStream();
           original_image.Save(maxms, System.Drawing.Imaging.ImageFormat.Jpeg);

 

            string thumbnail_id = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            Thumbnail thumb = new Thumbnail(thumbnail_id, ms.GetBuffer(), maxms.GetBuffer());

            //  把所有的都放到session中   
            List<Thumbnail> thumbnails = Session["file_info"] as List<Thumbnail>;
            if (thumbnails == null)
            {
                thumbnails = new List<Thumbnail>();
                Session["file_info"] = thumbnails;
            }
            thumbnails.Add(thumb);

            Response.StatusCode = 200;
            Response.Write(thumbnail_id);
        }
        catch
        {
            // 如果发生任何类型的错误返回一个500内部服务器错误

            Response.StatusCode = 500;
            Response.Write("An error occured");
            Response.End();
        }
        finally
        {
            // 清除
            if (final_image != null) final_image.Dispose();
            if (graphic != null) graphic.Dispose();
            if (original_image != null) original_image.Dispose();
            if (thumbnail_image != null) thumbnail_image.Dispose();
            if (ms != null) ms.Close();
            Response.End();
        }

    }
}
Thumbnail类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
///缩略图成员

/// </summary>
public class Thumbnail
{
    public Thumbnail(string id, byte[] data, byte[] minidata)
 {
  this.ID = id;
  this.Data = data;//缩略图信息流
        this.miniData = minidata;//原图信息流

 }


 private string id;
 public string ID
 {
  get
  {
   return this.id;
  }
  set
  {
   this.id = value;
  }
 }

 private byte[] thumbnail_data;
 public byte[] Data
 {
  get
  {
   return this.thumbnail_data;
  }
  set
  {
   this.thumbnail_data = value;
  }
 }

 

    private byte[] max_data;
    public byte[] miniData
    {
        get
        {
            return this.max_data;
        }
        set
        {
            this.max_data = value;
        }
    }   


 
}

posted @ 2012-11-29 07:52  飛燕88  阅读(420)  评论(1编辑  收藏  举报