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获取并验证后缀名。参考:http://hovertree.com/texiao/h/contenttype/

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

if (fileUpload_hovertree.PostedFile.ContentLength > 1048576)
 {
 literal_tips.Text = "选择的文件太大。";
 return;
 }

1048576字节也就是1M。

上传使用SaveAs方法就可以:

fileUpload_hovertree.SaveAs(h_fullName);

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

源码下载:

http://xiazai.jb51.net/201701/yuanma/hovertreetop.rar

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

您可能感兴趣的文章:

  • ASP.NET中上传并读取Excel文件数据示例
  • ASP.NET WebAPi(selfhost)实现文件同步或异步上传
  • Asp.net MVC中使用JQuery插件ajaxFileUpload上传文件
  • JQuery.uploadify 上传文件插件的使用详解 for ASP.NET
  • asp.net+FCKeditor上传图片显示叉叉图片无法显示的问题的解决方法
  • asp.net 多文件上传,兼容IE6/7/8,提供完整代码下载
  • asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
  • asp.net MVC实现无组件上传图片实例介绍
  • asp.net下文件上传和文件删除的代码
  • ASP.NET实现上传Excel功能
声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。