62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ WebHandler Language="c#" Class="File_WebHandler" Debug="true" %>
using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
public class File_WebHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
HttpPostedFile file = context.Request.Files[0];
if (file.ContentLength > 0 && file.ContentType.IndexOf("image/") >= 0)
{
int width = Convert.ToInt32(context.Request.Form["width"]);
int height = Convert.ToInt32(context.Request.Form["height"]);
string path = "data:image/jpeg;base64," + Convert.ToBase64String(ResizeImg(file.InputStream, width, height).GetBuffer());
context.Response.Write(path);
}
}
}
public MemoryStream ResizeImg(Stream ImgFile, int maxWidth, int maxHeight)
{
Image imgPhoto = Image.FromStream(ImgFile);
decimal desiredRatio = Math.Min((decimal)maxWidth / imgPhoto.Width, (decimal)maxHeight / imgPhoto.Height);
int iWidth = (int)(imgPhoto.Width * desiredRatio);
int iHeight = (int)(imgPhoto.Height * desiredRatio);
Bitmap bmPhoto = new Bitmap(iWidth, iHeight);
Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, imgPhoto.Width, imgPhoto.Height), GraphicsUnit.Pixel);
MemoryStream ms = new MemoryStream();
bmPhoto.Save(ms, ImageFormat.Jpeg);
imgPhoto.Dispose();
gbmPhoto.Dispose();
bmPhoto.Dispose();
return ms;
}
public bool IsReusable
{
get
{
return false;
}
}
}
<table class="perview">
<tr>
<th align="right"> 选择图片: </th>
<td width="75%"> <div id="idPicFile"> </div> </td>
</tr>
<tr>
<td colspan="2"><table>
<thead>
<tr>
<th> 文件路径 </th>
<th width="30%"> 预览图 </th>
<th width="20%"> 操作 </th>
</tr>
</thead>
<tbody id="idPicList">
<tr>
<td></td>
<td align="center"></td>
<td align="center"><a href="#">移除</a></td>
</tr>
</tbody>
</table></td>
</tr>
</table>
<script>
var table = $$("idPicList"), model = table.removeChild(table.rows[0]);
function AddPreview(){
var file = document.createElement("input"),
img = document.createElement("img"),
ip = new ImagePreview( file, img, {
maxWidth: 150,
maxHeight: 100,
action: "ImagePreview.ashx",
onErr: function(){ alert("载入预览出错!"); ResetFile(file); },
onCheck: CheckPreview,
onShow: ShowPreview
});
file.type = "file"; file.name = "pic";
file.onchange = function(){ ip.preview(); };
$$("idPicFile").appendChild(file);
}
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("您输入的用户名是:" + TextBox1.Text + "<br/>");
strMsg.Append("上传的文件分别是:<hr color='red'/>");
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
我在预览完之后 有个button上传按钮点击事件
string url = "";
//遍历file表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
string fileUrlPath = "~/admin/computerImgs/";//生成的文件夹要保存的位置
string time = DateTime.Now.ToString("yyyyMMdd");
string path = fileUrlPath + time + "/"; //完整路径
if (!Directory.Exists(Server.MapPath(path)))
{
Directory.CreateDirectory(Server.MapPath(path)); //创建文件夹
}
string imgUrl = path + fileName;
url += imgUrl + "|";
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(path) + fileName);
}
}