uploadfile圖片上傳和ashx


 

uploadify.swf需要是支持中文

  $(function () {
  //獲取所有上傳按鈕id
  $("div[id^='fileInput_']").each(function () {
  var name = $(this).attr("id");
  create(name);
  });
  });
   
  function create(name) {
  var select = "上傳圖片";
  if (name.indexOf("_") > -1) {
  if (name.substring(name.lastIndexOf("_") + 1) == "1") {
  var select = "大圖";
  }
  else if (name.substring(name.lastIndexOf("_") + 1) == "2") {
  select = "小圖";
  }
  }
  $('#' + name).uploadify({
  'uploader': '/uploadify/uploadify.swf',
  'script': 'Pro_Sub.aspx?upfile=1',
  'cancelImg': '/uploadify/cancel.png',
  'folder': '/ProductImg/',
  'fileExt': '*.jpg;*.gif;*.png;*.bmp;*.JPEG;',
  'fileDesc': '請選擇 *.jpg;*.gif;*.png,*.bmp;*.JPEG 格式的圖片',
  'multi': true,
  'auto': true,
  'buttonText':select,
  'onComplete': function (event, queueId, fileObj, response, data) {
  var re = response.split('|@|');
  if (re[0] == "1") {
  var box = name.substring(10);
  var html = "<div class='ImgHtml' OnClick='change(this)'><input class='input2' style='display: none;' type='button' value='刪&nbsp&nbsp&nbsp除' onclick='delImg(this);showfi("+name.replace("_",".")+");' /><input type='checkbox' checked='checked' class='hide' value='" + re[1] + "' /><img class='Img' src='" + re[1] + "'></div>";
  if (box == "1_0") {
  $("#ContentPlaceHolder1_rpAttribute_txtvalue_4").val(re[1]);
  html = "<div class='ImgHtml' OnClick='change(this)'><input class='input2' style='display: none;' type='button' value='刪&nbsp&nbsp&nbsp除' onclick='delImg(this);showfi(1)' /><input type='checkbox' class='hide' value='" + re[1] + "' /><img class='Img' src='" + re[1] + "'></div>";
  $("#file0").hide();
  }
   
  $(".box" + box).append(html);
   
  } else {
  ymPrompt.errorInfo({ message: "上傳失敗!", showMask: true });
  }
  }
  });
  }
   
 

</script>

 

 

=============================================================================

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

using System.Web.SessionState;
using UI;

namespace Admin.TkAdmin.Product
{
/// <summary>
/// imgsave 的摘要說明
/// </summary>
public class imgsave : IHttpHandler,IReadOnlySessionState
{
public HttpRequest Request;
public HttpResponse Response;
public HttpSessionState Session;
public void ProcessRequest(HttpContext context)
{

context.Response.ContentType = "text/plain";
Response = context.Response;
Request = context.Request;
Session = context.Session;

string upfile = Request["upfile"];
if (upfile != "")
{
//如果值為1zhi
if (upfile == "1")
{
uploadfile();
}
}

}

//對上傳的圖片進行保存到指定文件中
private void uploadfile()
{
HttpPostedFile file = Request.Files["Filedata"];
string UploadFile = "";
HttpContext.Current.Server.MapPath(@Request["folder"]);
string newFileName = "";
if (file != null)
{
Isfile(UploadFile);
try {
//創建隨機種子
Random ra;
if (Session["IntIndexImg"] != null)
{
ra = new Random(Convert.ToInt32(Session["IntIndexImg"]));
}
else
{
ra = new Random();
}

int rInt = ra.Next(1000);

Session["IntIndexImg"] = rInt;

newFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + rInt + Path.GetExtension(file.FileName).ToUpper();
string BigimageURL = (FileUrl.ProductBigImgUrl + newFileName).Replace('\\', '/');
Isfile(FileUrl.ProductBigImgUrl);
file.SaveAs(BigimageURL);
//保存縮列圖
string SmallimgeURL = (FileUrl.ProductSmallImgUrl + newFileName).Replace('\\', '/');
Isfile(FileUrl.ProductSmallImgUrl);
CreateSmallPic.CreateImageOutput(120, 60, BigimageURL, SmallimgeURL);
Response.Write("1|@|" + FileUrl.ProductSmallImgUrl1 + newFileName);

}catch
{
Response.Write("0");
}
}
}

//用於創建目錄
private void Isfile(string UploadFile)
{
if (!Directory.Exists(UploadFile))
{
Directory.CreateDirectory(UploadFile);
}

}

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM