uploadfile图片上传和ashx

时间:2023-12-15 21:17:50

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;
}
}
}
}