廢話不多說,:
上傳完畢自動添加進資料庫
使用方法:
在需要上傳的頁面head裡添加(css,圖片和js也要複製進來)
<link href="css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="swfupload/swfupload.js"></script> <script type="text/javascript" src="js/swfupload.queue.js"></script> <script type="text/javascript" src="js/fileprogress.js"></script> <script type="text/javascript" src="js/filegroupprogress.js"></script> <script type="text/javascript" src="js/handlers.js"></script> <script type="text/javascript"> var swfu; window.onload = function() { var settings = { flash_url: "swfupload/swfupload.swf", upload_url: "imgUpload.ashx",//資料提交處理頁面 file_size_limit: "2 MB",//大小限制 file_types: "*.jpg", //副檔名為jpg的檔案 file_types_description: "JPG Images",//jpg檔案 file_upload_limit: 100, file_queue_limit: 0, custom_settings: { progressTarget: "divprogresscontainer", progressGroupTarget : "divprogressGroup", //progress object container_css: "progressobj", icoNormal_css: "IcoNormal", icoWaiting_css: "IcoWaiting", icoUpload_css: "IcoUpload", fname_css : "fle ftt", state_div_css : "statebarSmallDiv", state_bar_css : "statebar", percent_css : "ftt", href_delete_css : "ftt", //sum object /* 頁面中不應出現以"cnt_"開頭聲明的元素 */ s_cnt_progress: "cnt_progress", s_cnt_span_text : "fle", s_cnt_progress_statebar : "cnt_progress_statebar", s_cnt_progress_percent: "cnt_progress_percent", s_cnt_progress_uploaded : "cnt_progress_uploaded", s_cnt_progress_size : "cnt_progress_size" }, debug: false, // Button settings button_image_url: "images/TestImageNoText_65x29.png", button_width: "65", button_height: "29", button_placeholder_id: "spanButtonPlaceHolder", button_text: '<span class="theFont">上傳檔案</span>', button_text_style: ".theFont { font-size: 12;color:#0068B7; }", button_text_left_padding: 12, button_text_top_padding: 3, // The event handler functions are defined in handlers.js file_queued_handler: fileQueued, file_queue_error_handler: fileQueueError, upload_start_handler: uploadStart, upload_progress_handler: uploadProgress, upload_error_handler: uploadError, upload_success_handler: uploadSuccess, upload_complete_handler:uploadComplete, file_dialog_complete_handler: fileDialogComplete }; swfu = new SWFUpload(settings); }; </script>
在具體要上傳的地方放如下代碼
<span id="spanButtonPlaceHolder"></span><div id="divprogresscontainer"> </div> <div id="divprogressGroup"> </div>
添加一個一般處理常式(imgUpload.ashx),用來接受上傳的圖片存到資料庫
<%@ WebHandler Language="C#" Class="imgUpload" %>using System;using System.Collections.Generic;using System.Web;using System.IO;public class imgUpload : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.Cookies["user"]==null) { jiangs_Tools.Show_Msg("請重新登入"); } 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; file.SaveAs(HttpContext.Current.Server.MapPath("Uploadfile/" + Path.GetFileName(file.FileName)));//儲存圖片 string newname = sjname() + Path.GetExtension(file.FileName);//產生新名稱 File.Move(context.Server.MapPath("Uploadfile/" + Path.GetFileName(file.FileName)), context.Server.MapPath("Uploadfile/" + newname)); //重新命名 jiang_Db newdb = new jiang_Db(); newdb.Open(); newdb.ExecSql(string.Format("insert into [img] ([name],[img],[member_id]) values ('{0}','{1}','{2}')", //存入資料庫 Path.GetFileNameWithoutExtension(file.FileName),"/Uploadfile/"+newname,context.Request.Cookies["user"]["id"])); newdb.Close(); //File.AppendAllText(context.Server.MapPath("filename.txt"), file.FileName+" 新名稱:"+newname + Environment.NewLine); } } catch (Exception ex) { context.Response.StatusCode = 500; context.Response.Write(ex.Message); context.Response.End(); } finally { context.Response.End(); } } public bool IsReusable { get { return false; } } /// <summary> /// 產生個隨即名稱 /// </summary> /// <returns></returns> public string sjname() { string sj = null; sj = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.TimeOfDay.Hours.ToString() + DateTime.Now.TimeOfDay.Minutes.ToString() + DateTime.Now.TimeOfDay.Milliseconds.ToString(); return sj; }}