一个html上传控件 <input type="file" runat="server" id="fileuploadid" style="width: 439px" />
后台: if (fileuploadid.PostedFile.FileName != "")
{
savePath = ""; // DateTime.Now.ToString("yyyyMM")+"/"; //指定上传附件保存的目录,根据年月生成 如200804/
Exception excp = InitUploadSaveDirectory(savePath);
//如果返回异常则退出程序
if (excp != null)
{
Response.Write(excp.ToString());
return;
}
filePath = fileuploadid.PostedFile.FileName;
fileName = Path.GetFileName(filePath); //原始附件名称
fileSize = fileuploadid.PostedFile.ContentLength; //附件大小
tt = fileName.LastIndexOf(".");
fileType = fileName.Substring(tt).ToLower(); //附件格式,小写
//获取服务端附件保存根目录,如D:\上传\upfiles\200804/
sAbsoluteSavePath = Path.Combine(Request.MapPath(UploadSavePath), savePath);
//指定附件保存在服务器上的名称(根本上传时间生成,重名处理)
sFileSaveName = DateTime.Now.ToString("MMdd") + DateTime.Now.ToString("hhmmss") + fileType; if (fileSize > 0 && fileType.Length > 0 && ("|" + UploadFileTypes + "|").IndexOf("|" + fileType + "|") > -1)
{
try
{
string sqlInsert;
fileuploadid.PostedFile.SaveAs(Path.Combine(sAbsoluteSavePath, sFileSaveName));
SqlConnection sqlcon = new SqlConnection(strConn);
sqlInsert = "insert into UploadFiles(FileName,OrigialName,SavePath,FileSize,Uptime) values('" + sFileSaveName + "','" + fileName + "','" + sAbsoluteSavePath + "','" + fileSize + "','" + DateTime.Now.ToString("yyyy - MM - dd") + "')";
SqlCommand sqlcom = new SqlCommand(sqlInsert,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
Response.Write("<script>alert('上传成功!');</script>"); }
catch (Exception ex)
{
Response.Write("错误:" +ex.Message);
}
}
}
else
{
Response.Write("<script>alert('空的不能传的!');</script>");
}
我现在的要求是:将所有上传的图片都搞成一样的大小,有就是长啊,宽都限定!怎么来处理这个: fileSize = fileuploadid.PostedFile.ContentLength; //附件大小 呵呵 小弟在线等
后台: if (fileuploadid.PostedFile.FileName != "")
{
savePath = ""; // DateTime.Now.ToString("yyyyMM")+"/"; //指定上传附件保存的目录,根据年月生成 如200804/
Exception excp = InitUploadSaveDirectory(savePath);
//如果返回异常则退出程序
if (excp != null)
{
Response.Write(excp.ToString());
return;
}
filePath = fileuploadid.PostedFile.FileName;
fileName = Path.GetFileName(filePath); //原始附件名称
fileSize = fileuploadid.PostedFile.ContentLength; //附件大小
tt = fileName.LastIndexOf(".");
fileType = fileName.Substring(tt).ToLower(); //附件格式,小写
//获取服务端附件保存根目录,如D:\上传\upfiles\200804/
sAbsoluteSavePath = Path.Combine(Request.MapPath(UploadSavePath), savePath);
//指定附件保存在服务器上的名称(根本上传时间生成,重名处理)
sFileSaveName = DateTime.Now.ToString("MMdd") + DateTime.Now.ToString("hhmmss") + fileType; if (fileSize > 0 && fileType.Length > 0 && ("|" + UploadFileTypes + "|").IndexOf("|" + fileType + "|") > -1)
{
try
{
string sqlInsert;
fileuploadid.PostedFile.SaveAs(Path.Combine(sAbsoluteSavePath, sFileSaveName));
SqlConnection sqlcon = new SqlConnection(strConn);
sqlInsert = "insert into UploadFiles(FileName,OrigialName,SavePath,FileSize,Uptime) values('" + sFileSaveName + "','" + fileName + "','" + sAbsoluteSavePath + "','" + fileSize + "','" + DateTime.Now.ToString("yyyy - MM - dd") + "')";
SqlCommand sqlcom = new SqlCommand(sqlInsert,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
Response.Write("<script>alert('上传成功!');</script>"); }
catch (Exception ex)
{
Response.Write("错误:" +ex.Message);
}
}
}
else
{
Response.Write("<script>alert('空的不能传的!');</script>");
}
我现在的要求是:将所有上传的图片都搞成一样的大小,有就是长啊,宽都限定!怎么来处理这个: fileSize = fileuploadid.PostedFile.ContentLength; //附件大小 呵呵 小弟在线等
解决方案 »
- 如何提取所有图像文件
- SQL 如何根据指定值获取列名? sql高手进!!!!
- 急。。RadioButtonList 怎么给绑定值,进来看详细介绍
- 全部分数奉送(167分),哪位大哥能给我一份以Window服务作为WCF寄主的DEMO,谢谢!
- 怎样将DataGrid中的Combox连接到数据表中的字段
- 动态添加控件,急呀
- 服务器报:windows socket error: 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作(10055),on API connect
- 各位高手,帮忙看个问题,在线等!!!
- 如何处理Session的保留时间?
- Asp.net每周源码(16个)——来者无分
- vb.net 如何在更新(删除)数据库(access)中图片名的同时,删除文件夹中的同名图片?
- iis 里asp.net 文件目录小问题。
bmp = new Bitmap(width, height);
g = Graphics.FromImage(bmp);
g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
bmp.Save(...);
大概就这样吧,应该能满足你的要求
private bool GetPicWidth(HtmlInputFile Pic)
{
Helper helper = new Helper();
HttpPostedFile UpFile = Pic.PostedFile;
Byte[] FileByteArray = new byte[UpFile.ContentLength];
Stream StreamObject = UpFile.InputStream;
StreamObject.Read(FileByteArray, 0, UpFile.ContentLength);
string Base64String = ByteToBase64(FileByteArray, 0, FileByteArray.Length); System.Drawing.Image myImage = System.Drawing.Image.FromStream(StreamObject); bool CheckImageResult = CheckImage(Convert.ToInt32(myImage.Width), Convert.ToInt32(myImage.Height));
if (CheckImageResult == false)
{
helper.Alert("图片尺寸过大!请上传宽小于600的图片", Page);
return false;
}
else
{
return true;
} }
private bool CheckImage(int Width, int Height)
{
bool Result = true;
int width = 600, height = 10;
if (Width > width)
{
Result = false;
}
return Result;
}
//这里是进行二进制到Base64的转换
private string ByteToBase64(byte[] ByteArry, int Start, int Length)
{
string Result;
Result = System.Convert.ToBase64String(ByteArry, Start, Length);
return Result;
}
#endregion
////直接引用
//文件大於4M將不能上載
function CheckFileSize(obj)
{
if(obj==null)
obj=window.event.srcElement;
var id=obj.id;
var maxLength=4096000;//4M
var fso = new ActiveXObject('Scripting.FileSystemObject');
if(obj.value.trim()=="")
return;
if(fso.FileExists(obj.value))
{
var file = fso.GetFile(obj.value);
if(file.Size>maxLength)
{
alert('The size of attachment exceed 4Mb.');
obj.outerHTML=obj.outerHTML;
document.getElementById(id).attachEvent("onkeydown",function() {CheckKeyDown(document.getElementById(id));});
document.getElementById(id).attachEvent("onchange",function() {CheckFileSize(document.getElementById(id));});
}
}
else
{
alert('File does not exist.');
obj.outerHTML=obj.outerHTML;
document.getElementById(id).attachEvent("onkeydown",function() {CheckKeyDown(document.getElementById(id));});
document.getElementById(id).attachEvent("onchange",function() {CheckFileSize(document.getElementById(id));});
}
}//Backspace & Delete鍵有效,並且將FileField內容清空
function CheckKeyDown(obj)
{
if(obj==null)
obj=window.event.srcElement;
var id=obj.id;
if(window.event.keyCode!=8&&window.event.keyCode!=46)
{
window.event.returnValue=false;
}
else
{
window.event.returnValue=false;
obj.outerHTML=obj.outerHTML;
document.getElementById(id).attachEvent("onkeydown",function() {CheckKeyDown(document.getElementById(id));});
document.getElementById(id).attachEvent("onchange",function() {CheckFileSize(document.getElementById(id));});
}
}if (document.all)
{
window.attachEvent("onload",function(){AddEvent();})
}
else
{
window.addEventListener("load",function(){AddEvent();},false);
}function AddEvent()
{
var input_file=document.getElementsByTagName("INPUT");
for (var i=0; i<input_file.length; i++)
{
if (input_file[i].type=="file")
{
input_file[i].attachEvent("onkeydown",function() {CheckKeyDown(null);});
input_file[i].attachEvent("onchange",function() {CheckFileSize(null);});
}
}
}String.prototype.trim= function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
/// 生成缩略图
public void MakeThumbnail(string originalImagePath,string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage =System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW":
break;
case "W":
toheight = originalImage.Height * width/originalImage.Width;
break;
case "H":
towidth = originalImage.Width * height/originalImage.Height;
break;
case "A":
if (originalImage.Width / originalImage.Height >= width / height)
{
if (originalImage.Width > width)
{
towidth = width;
toheight = (originalImage.Height * width) / originalImage.Width;
}
else
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
}
else
{
if (originalImage.Height > height)
{
toheight = height;
towidth = (originalImage.Width * height) / originalImage.Height;
}
else
{
towidth = originalImage.Width;
toheight = originalImage.Height;
}
}
break;
case "Cut":
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height*towidth/toheight;
y = 0;
x = (originalImage.Width - ow)/2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width*height/towidth;
x = 0;
y = (originalImage.Height - oh)/2;
}
break;
default :
break;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.Clear(Color.Transparent); g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel); try
{
bitmap.Save(Server.MapPath("file/" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}参考