asp.net中在ashx文件里获得客户端上传的完整路径有关问题
asp.net中在ashx文件里获得客户端上传的完整路径问题
------解决方案--------------------
生成缩略图要客户端路径干嘛?需要的是服务器路径
另外,不要路径也能生成缩略图
不经保存,直接生成上传图片的等比例的高质量缩略图
------解决方案--------------------
ashx中的方法
HttpPostedFile fileValue = context.Request.Files["FileValue"];//FileValue是前台file控件的id
//如果我想获得客户选择文件的客户端的完整路径
string test = fileValue.FileName;
//这样写我在ie浏览器中断点调试可以完全获得客户端的路径,我在ff或谷歌浏览器调试错误,只能获得文件名。不知道为什么。求解……
------解决方案--------------------
生成缩略图要客户端路径干嘛?需要的是服务器路径
另外,不要路径也能生成缩略图
不经保存,直接生成上传图片的等比例的高质量缩略图
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
String fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(FileUpload1.FileBytes));
int newWidth = 300, newHeight = 200;
if ((decimal)image.Width / image.Height > (decimal)newWidth / newHeight)
{
newHeight =Convert.ToInt32((decimal)image.Height * newWidth / image.Width);
}
else if ((decimal)image.Width / image.Height < (decimal)newWidth / newHeight)
{
newWidth = Convert.ToInt32((decimal)image.Width * newHeight / image.Height);
}
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);
g.DrawImage(image, rectDestination, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
bmp.Save(Server.MapPath("~/") + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt);
bmp.Dispose();
image.Dispose();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>不经保存,直接生成上传图片的等比例的高质量缩略图</title>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传并生成缩略图" />
</form>
</body>
</html>
------解决方案--------------------
ashx中的方法
<%@ WebHandler Language="C#" Class="UploadFile" Debug="true" %>
using System;
using System.Web;
public class UploadFile : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
HttpPostedFile f1 = context.Request.Files["f1"];
String fileExt = System.IO.Path.GetExtension(f1.FileName);
System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);
int newWidth = 300, newHeight = 200;
if ((decimal)image.Width / image.Height > (decimal)newWidth / newHeight)
{
newHeight = Convert.ToInt32((decimal)image.Height * newWidth / image.Width);
}
else if ((decimal)image.Width / image.Height < (decimal)newWidth / newHeight)
{
newWidth = Convert.ToInt32((decimal)image.Width * newHeight / image.Height);
}
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(newWidth, newHeight);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, newWidth, newHeight);