Asp.net+swfupload Multi-image batch upload (with source download)

Source: Internet
Author: User
Tags httpcontext

ASP. net file upload is a single file upload method, can not perform a one-time multi-image batch upload operation, to achieve multi-image batch upload need to rely on flash, through flash to select multiple pictures (files), and then through the back-end services to upload operations.

This tutorial uses the Flash upload file is swfupload, below will have the source download link.

Using Tools vs 2010.

Demonstrate

First step to create a new Web project

The second step is to introduce the required Swfuplod files (SWFUPLOAD.SWF,JS,CSS, etc.)

Step three create a new generic handler (UPLOAD.ASHX)

UPLOAD.ASHX program file Code

Using system;using system.collections.generic;using system.web;using System.IO;    namespace yuyue.upload{//<summary>///Upload Summary description///</summary> public class Upload:ihttphandler { public void ProcessRequest (HttpContext context) {context.      Response.ContentType = "Text/plain";             try {Httppostedfile file; for (int i = 0; I < context. Request.Files.Count; ++i) {file = context.                Request.files[i]; if (file = = NULL | | file. ContentLength = = 0 | | String. IsNullOrEmpty (file.                FileName)) continue; string filename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + RNDNUMSTR (6) + path.getextension (file. FileName); File name = Upload Date + random string + extension (to avoid multiple uploads being the first problem)/******************** folder **************************/Str                ing year=datetime.now.year.tostring ();                String monthday = DateTime.Now.ToString ("MMdd"); if (! Directory.Exists (HttpContext.Current.ServeR.mappath ("/uploads/") +year)) {Directory.CreateDirectory (HttpContext.Current.Server.MapPa               Th ("/uploads/") + year); } if (!                   Directory.Exists (HttpContext.Current.Server.MapPath ("/uploads/") + year + "/" + MonthDay) {               Directory.CreateDirectory (HttpContext.Current.Server.MapPath ("/uploads/") + year + "/" + MonthDay); } file.               SaveAs (HttpContext.Current.Server.MapPath ("/uploads/" + year + "/" + MonthDay + "/" + filename)); Context.  Response.Write ("/uploads/" + year + "/" + MonthDay + "/" + filename);//output upload address for foreground JS get}} catch        (Exception ex) {context.           Response.statuscode = 500; Context. Response.Write (ex.           Message); Context.        Response.End (); } finally {context.        Response.End (); }} #region This method is used to generate a random string of the specified number of digits///<summary>////////</summary> <param name= "Vcodenum" > parameter is the number of digits of the random number </param>//<returns> Returns a random number string </returns> public static  String rndnumstr (int vcodenum) {string[] Source = {"0", "1", "1", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C",       "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "Z"};      string checkcode = String.Empty;      Random random = new random (); for (int i = 0; i < Vcodenum; i++) {Checkcode + = Source[random. Next (0, source.      Length)]; } return Checkcode;    } #endregion public bool IsReusable {get {return false; } } }}
Fourth step create a new Web Form (swfupload.aspx)

swfupload.aspx File Source

<%@ page language= "C #" autoeventwireup= "true" codebehind= "Swfupload.aspx.cs" inherits= "yuyue.upload.swfupload"% > <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

Swfupload.aspx.cs File Source

Using system;using System.Collections.Generic; Using system.web;using system.web.ui;using System.Web.UI.WebControls; namespace yuyue.upload{public partial class Swfupload:System.Web.UI.Page {protected void Page_Load (object sender, even Targs e) {if (! IsPostBack) {//construct swfupload.swf required js file Literal1.text + = "<script type=\" text/javascript\ ">\n";  Literal1.text + = "var swfu;\n"; Literal1.text + = "window.onload = function () {\ n"; Literal1.text + = "var settings = {\ n"; Literal1.text + = "Flash_url: \"/statics/js/swfupload/swfupload.swf\ ", \ n"; Literal1.text + = "Upload_url: \" upload.ashx\ ", \ n"; if (request.querystring["File_size_limit"]! = null) {Literal1.text + = "File_size_limit: \" "+ request.querystring[" file _size_limit "] +" \ "\ n";//File size limit Literal3.text = request.querystring["File_size_limit"]; } else {literal1.text + = "File_size_limit: \" 2 mb\ ", \ n"; Literal3.text = "2 MB";//File size limit} if (request.querystring["File_Types"]! = null) {Literal1.text + = "File_Types: \" " + request.querystring["File_Types"] + "\", \ n "; Literal4.text = request.querystring["File_Types"]; } else {literal1.text + = "File_Types: \" *.*\ ", \ n"; Literal4.text = "* *"; if (request.querystring["file_types_description"]! = null) {Literal1.text + = "file_types_description: \" "+ Request.Q uerystring["file_types_description"] + "\", \ n ";  } else {literal1.text + = "file_types_description: \" All files\ ", \ n";} if (request.querystring["File_upload_limit"]! = null) {Literal1.text + = "File_upload_limit:" + request.querystring["fi Le_upload_limit "] +", \ n "; Literal2.text = request.querystring["File_upload_limit"]; Literal2.text = "50";   } else {literal1.text + = "file_upload_limit:50,\n";} Literal1.text + = "file_queue_limit:0,\n"; Literal1.text + = "autoremove:true,//whether to automatically remove the completed upload record \ n";  Literal1.text + = "Custom_settings: {\ n"; Literal1.text + = "Progresstarget: \" Divprogresscontainer\ ", \ n";   Literal1.text + = "Progressgrouptarget: \" Divprogressgroup\ ", \ n"; Progress Object Literal1.text + = "Container_css: \" progressobj\ ", \ n"; Literal1.text + = "Iconormal_css: \" Iconormal\ ", \ n"; Literal1.text + = "Icowaiting_css: \" icowaiting\ ", \ n"; Literal1.text + = "Icoupload_css: \" icoupload\ ", \ n"; Literal1.text + = "Fname_css: \" Fle ftt\ ","; Literal1.text + = "State_div_css: \" statebarsmalldiv\ ", \ n"; Literal1.text + = "Vstate_bar_css: \" Statebar\ ", \ n"; Literal1.text + = "Percent_css: \" ftt\ ", \ n";   Literal1.text + = "Href_delete_css: \" ftt\ ", \ n"; The element declared at the beginning of "Cnt_" should not appear in the Sum object/* page */Literal1.text + = "s_cnt_progress: \" Cnt_progress\ ", \ n"; Literal1.text + = "S_cnt_span_text: \" fle\ ","; Literal1.text + = "S_cnt_progress_statebar: \" Cnt_progress_statebar\ ", \ n"; Literal1.text + = "s_cnt_progress_percent: \" cnt_progress_percent\ ", \ n"; Literal1.text + = "s_cnt_progress_uploaded:\" cnt_progress_uploaded\ ", \ n"; Literal1.text + = "S_cnt_progress_size: \" cnt_progress_size\ "\ n"; Literal1.text + = "},\n"; Literal1.text + = "debug:false,\n"; Button Settings Literal1.text+ = "Button_image_url: \"/statics/js/swfupload/images/swfbnt_select.png\ ", \ n"; Literal1.text + = "Button_width: \" 75\ ", \ n"; Literal1.text + = "Button_height: \" 28\ ", \ n"; Literal1.text + = "button_placeholder_id: \" Spanbuttonplaceholder\ ", \ n"; Button_cursor= "HAND",//button_text: ",//button_text_style:". Thefont {font-size:12;color: #0068B7;} ",//button_ Text_left_padding:12,//button_text_top_padding:3,//The event handler functions is defined in Handlers.js Literal1 . Text + = "file_queued_handler:filequeued,\n"; Literal1.text + = "file_queue_error_handler:filequeueerror,\n"; Literal1.text + = "upload_start_handler:uploadstart,\n"; Literal1.text + = "upload_progress_handler:uploadprogress,\n"; Literal1.text + = "upload_error_handler:uploaderror,\n"; Literal1.text + = "upload_success_handler:uploadsuccess,\n"; Literal1.text + = "upload_complete_handler:uploadcomplete\n"; File_dialog_complete_handler:filedialogcomplete Literal1.text + = "};\n"; Literal1.text + = "SWFu = new swfupload (settings); \ n "; Literal1.text + = "};\n"; Literal1.text + = "</script>\n"; } } }}
Download source code ASP (C #) +flash (swfupload) multi-image bulk upload source Download summary

SWFUpload multi-File upload can be used for many types of file upload, here only shows the picture, you can expand yourself.

Similarly, swfupload is not dependent on a platform or language, the same can be used in php,java,asp.net and other languages, as long as you find out the principle can be arbitrarily extended, if there is any do not understand can give me a message.

Asp.net+swfupload Multi-image batch upload (with source download)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.