当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net上传图片到服务器方法详解

asp.net上传图片到服务器方法详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

三圾片目录大全下载,滕县保卫战幸存者,美女托衣小游戏

asp.net的fileupload控件可用于上传文件到服务器。hovertreetop新增了一个“阅图”功能,图片就是用fileupload上传的。

这里要说明的是上传图片限定文件名和文件大小等代码。

文件上传功能使用用户控件实现,在hovertreepanel项目中的htpanel\hcontrol\ucpictureadd.ascx 控件,

hovertreetop上传的图片文件暂时限定为jpg、png和gif。代码为:

<asp:fileupload runat="server" id="fileupload_hovertree" clientidmode="static" accept="image/png,image/jpeg,image/gif" />

c#代码:

htpictureinfo h_info = new htpictureinfo();
 h_info.htsuffix = hovertreeimagetool.getgpjimagefileextension(fileupload_hovertree.postedfile.contenttype);
 if (h_info.htsuffix == "")
 {
 literal_tips.text = "请选择jpg,png或者gif图片文件";
 return;
 }

其中getgpjimagefileextension方法在hovertreeframe项目中,代码:

namespace hovertree.hovertreeframe.htimage
{
 public class hovertreeimagetool
 {
 /// <summary>
 /// 根据图片文件的mime内容类型获取文件的后缀名,如果不是gif,png或者jpg图片文件则返回空字符串
 /// http://hovertree.com/h/bjag/viv8qlpx.htm
 /// http://hovertree.com/texiao/h/contenttype/
 /// </summary>
 /// <param name="contenttype"></param>
 /// <returns></returns>
 public static string getgpjimagefileextension(string contenttype)
 {
 switch (contenttype)
 {
 case "image/jpeg":
 return "jpg";
 case "image/pjpeg":
 return "jpg";
 case "image/gif":
 return "gif";
 case "image/png":
 return "png";
 case "image/x-png":
 return "png";
 default:
 return string.empty;
 }
 }
 }
}

也就是使用contenttype获取并验证后缀名。参考:

还有一个就是限定上传文件的大小,暂时限定为1m,代码如下:

if (fileupload_hovertree.postedfile.contentlength > 1048576)
 {
 literal_tips.text = "选择的文件太大。";
 return;
 }

1048576字节也就是1m。

上传使用saveas方法就可以:

fileupload_hovertree.saveas(h_fullname);

其中h_fullname为完整文件名字符串。

源码下载:

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网