只允许上传 GIF JPG类型的文件

ywlang2012 2012-06-05 09:14:53
只允许上传 GIF JPG类型的文件 我在本地调试的时候是好的,但是放到服务器上就出现这个问题,都上传的同一张图片
...全文
702 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
代码小天王 2012-06-05
  • 打赏
  • 举报
回复
有区别么 反正到客户端都是input 服务端都是InputStream
[Quote=引用 12 楼 的回复:]

哎 你要用vs提供的控件浏览上传?还是自定义控件上传
[/Quote]
刹那的菜鸟 2012-06-05
  • 打赏
  • 举报
回复
哎 你要用vs提供的控件浏览上传?还是自定义控件上传
  • 打赏
  • 举报
回复
if (hpf.ContentType == "image/pjpeg" || hpf.ContentType == "image/gif")
{
//
}
else{
Response.Write("只允许上传 GIF JPG类型的文件");
return;
}

不应该是这么写吗??
代码小天王 2012-06-05
  • 打赏
  • 举报
回复
现在客户端判断后缀名,然后在服务端去判断mime
ywlang2012 2012-06-05
  • 打赏
  • 举报
回复
解决 谢谢大侠们的指点。。我是这样做的
if (hpf.ContentType != "image/pjpeg" && hpf.ContentType != "image/gif" && hpf.ContentType != "image / jpeg") 多加了判断
陌城灬流年閣 2012-06-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
点击上传按钮事件:
protected void Button2_Click(object sender, EventArgs e)
{
string extend=this.fileup.FileName.Substring(this.fileup.FileName.LastIndexOf("."));//取出上传文件的后缀名
if (extend.ToLower() =……
[/Quote]这种的你可以试试
陌城灬流年閣 2012-06-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
点击上传按钮事件:
protected void Button2_Click(object sender, EventArgs e)
{
string extend=this.fileup.FileName.Substring(this.fileup.FileName.LastIndexOf("."));//取出上传文件的后缀名
if (extend.ToLower() =……
[/Quote]++
htbyrd 2012-06-05
  • 打赏
  • 举报
回复
太难了!!!
net5354 2012-06-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
点击上传按钮事件:
protected void Button2_Click(object sender, EventArgs e)
{
string extend=this.fileup.FileName.Substring(this.fileup.FileName.LastIndexOf("."));//取出上传文件的后缀名
if (extend.ToLower() =……
[/Quote]++
AneurinSun 2012-06-05
  • 打赏
  • 举报
回复
点击上传按钮事件:
protected void Button2_Click(object sender, EventArgs e)
{
string extend=this.fileup.FileName.Substring(this.fileup.FileName.LastIndexOf("."));//取出上传文件的后缀名
if (extend.ToLower() == ".gif" || extend.ToLower() == ".jpg")
{
上传后的处理
}
else
{
ClientScript.RegisterStartupScript(this.GetType(),"tet","alert('请确定上传图片类型是否正确')",true);
}

}
ywlang2012 2012-06-05
  • 打赏
  • 举报
回复
HttpPostedFile hpf = FileUpload1.PostedFile;


//取得文件名(不含路径)

char[] splitChar = { };

string[] FilenameArray = hpf.FileName.Split(splitChar);

string Filename = FilenameArray[FilenameArray.Length - 1].ToLower();



if (hpf.FileName.Length < 1)
{

Response.Write("请选择您要上传的图片文件");

return;

}

if (hpf.ContentType != "image/pjpeg" && hpf.ContentType != "image/gif")
{

Response.Write("只允许上传 GIF JPG类型的文件");

return;

}

else
{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append(DateTime.Now.Year.ToString());

sb.Append(DateTime.Now.Month.ToString());

sb.Append(DateTime.Now.Day.ToString());

sb.Append(DateTime.Now.Hour.ToString());

sb.Append(DateTime.Now.Minute.ToString());

sb.Append(DateTime.Now.Second.ToString());

if (Filename.ToLower().EndsWith("gif"))
{

sb.Append(".gif");

}

else if (Filename.ToLower().EndsWith("jpg"))
{

sb.Append(".jpg");

}

else if (Filename.ToLower().EndsWith("jpeg"))
{

sb.Append(".jpeg");

}

Filename = sb.ToString();

}



// 保存图片到服务器上

try
{

hpf.SaveAs(Server.MapPath("\\") + "/newImage/" + Filename);

string BigPic = "/newImage/" + Filename;
model.ybcs_SamllPic = BigPic;



}

catch (Exception ee)
{

Response.Write("上传图片失败,原因" + ee.Message);

return;

}





// 生成缩略图

//原始图片名称

string originalFilename = hpf.FileName;

//生成的高质量图片名称

string strFile = Server.MapPath("/newImage/Small_" + Filename);
string small_image = "/newImage/Small_" + Filename;

model.ybcs_spPic = small_image;


//从文件取得图片对象

System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream, true);



Double Width = Double.Parse(TextBox26.Text.Trim());

Double Height = Double.Parse(TextBox27.Text.Trim());

System.Double NewWidth, NewHeight;



if (image.Width > image.Height)
{

NewWidth = Width;

NewHeight = image.Height * (NewWidth / image.Width);

}

else
{

NewHeight = Height;

NewWidth = (NewHeight / image.Height) * image.Width;

}



if (NewWidth > Width)
{

NewWidth = Width;

}

if (NewHeight > Height)
{

NewHeight = Height;

}



System.Drawing.Size size = new Size((int)NewWidth, (int)NewHeight); // 图片大小

System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height); //新建bmp图片

System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); //新建画板

graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法

graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //设置高质量,低速度呈现平滑程度

graphics.Clear(Color.White); //清空画布

//在指定位置画图

graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),

new System.Drawing.Rectangle(0, 0, image.Width, image.Height),

System.Drawing.GraphicsUnit.Pixel);



//文字水印

System.Drawing.Graphics textGraphics = System.Drawing.Graphics.FromImage(bitmap);

System.Drawing.Font font = new Font("宋体", 10);

System.Drawing.Brush brush = new SolidBrush(Color.Black);

textGraphics.DrawString(TextBox28.Text.Trim(), font, brush, 10, 10);

textGraphics.Dispose();




try
{

bitmap.Save(strFile, System.Drawing.Imaging.ImageFormat.Jpeg);

}

catch (Exception ex)
{

Response.Write("保存缩略图失败:" + ex.Message);

}





graphics.Dispose();

image.Dispose();

bitmap.Dispose();


  • 打赏
  • 举报
回复
代码贴出来 睇睇!
tangserver 2012-06-05
  • 打赏
  • 举报
回复
代码呢?

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧