1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;
  5. using System.Web;
  6. using Bo_myCommon;
  7.  
  8. public class Upload
  9. {
  10. #region 上传图片
  11.  
  12. /// <summary>
  13. /// 上传图片
  14. /// </summary>
  15. /// <param name="imgBuffer">字节数组</param>
  16. /// <param name="uploadpath">保存路径。绝对或虚拟路径</param>
  17. /// <param name="imgformat">图片保存格式</param>
  18. /// <returns>上传成功后返回的新的文件名</returns>
  19. public static string UploadImage(byte[] imgBuffer, string uploadpath, ImageFormat imgformat)
  20. {
  21. try
  22. {
  23. System.IO.MemoryStream m = new MemoryStream(imgBuffer);
  24.  
  25. if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath)))
  26. Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath));
  27.  
  28. string imgname = StringHelper.CreateIDCode() + "." + imgformat.ToString().ToLower();
  29.  
  30. string _path = HttpContext.Current.Server.MapPath(uploadpath) + imgname;
  31.  
  32. Image img = System.Drawing.Image.FromStream(m);
  33. img.Save(_path, imgformat);
  34. m.Close();
  35.  
  36. return uploadpath + imgname;
  37. }
  38. catch (Exception ex)
  39. {
  40. return ex.Message;
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// 上传图片
  46. /// </summary>
  47. /// <param name="stream">Stream</param>
  48. /// <param name="uploadpath">保存路径。绝对或虚拟路径</param>
  49. /// <param name="imgformat">图片保存格式</param>
  50. /// <returns>上传成功后返回的新的文件名</returns>
  51. public static string UploadImage(Stream stream, string uploadpath, ImageFormat imgformat)
  52. {
  53. try
  54. {
  55. Image img = Image.FromStream(stream);
  56. string filename = StringHelper.CreateIDCode() + "." + imgformat.ToString().ToLower();
  57. filename = HttpContext.Current.Server.MapPath(uploadpath) + filename;
  58. img.Save(filename, imgformat);
  59. return filename;
  60. }
  61. catch (Exception ex)
  62. {
  63. return ex.Message;
  64. }
  65. }
  66.  
  67. /// <summary>
  68. /// 上传图片
  69. /// </summary>
  70. /// <param name="postfile">客户端上传的文件</param>
  71. /// <param name="uploadpath">保存地址</param>
  72. /// <param name="imgformat">图片格式</param>
  73. /// <returns></returns>
  74. public static string UploadImage(HttpPostedFile postfile, string uploadpath, ImageFormat imgformat)
  75. {
  76. switch (imgformat.ToString().ToLower())
  77. {
  78. case "jpeg":
  79. return UploadImageForJPEG(postfile, uploadpath);
  80. case "bmp":
  81. return UploadImageForBMP(postfile, uploadpath);
  82. case "png":
  83. return UploadImageForPNG(postfile, uploadpath);
  84. case "gif":
  85. return UploadImageForGIF(postfile, uploadpath);
  86. default:
  87. return UploadImageForJPEG(postfile, uploadpath);
  88. }
  89. }
  90.  
  91. /// <summary>
  92. /// 上传图片,保存为JPEG格式
  93. /// </summary>
  94. /// <param name="postfile">HttpPostedFile</param>
  95. /// <param name="uploadpath">保存文件地址</param>
  96. /// <returns>返回上传后的路径</returns>
  97. public static string UploadImage(HttpPostedFile postfile, string uploadpath, bool autoImageName)
  98. {
  99. if (autoImageName)
  100. {
  101. switch (Path.GetExtension(postfile.FileName).ToLower())
  102. {
  103. case ".jpg":
  104. return UploadImageForJPEG(postfile, uploadpath);
  105. case ".gif":
  106. return UploadImageForGIF(postfile, uploadpath);
  107. case ".png":
  108. return UploadImageForPNG(postfile, uploadpath);
  109. default:
  110. return UploadImageForJPEG(postfile, uploadpath);
  111. }
  112. }
  113. else
  114. {
  115. Image img = Image.FromStream(postfile.InputStream);
  116. ImageHelper.ZoomAuto(postfile, uploadpath, img.Width, img.Height, "", "", null);
  117. return uploadpath;
  118. }
  119. }
  120.  
  121. /// <summary>
  122. /// 自动生成新的图片名称
  123. /// </summary>
  124. /// <param name="postfile"></param>
  125. /// <param name="uploadpath"></param>
  126. /// <returns></returns>
  127. public static string UploadImage(HttpPostedFile postfile, string uploadpath)
  128. {
  129. return UploadImage(postfile, uploadpath, true);
  130. }
  131.  
  132. #region 水印
  133.  
  134. #region 上传图片,不缩放,并添加文字水印
  135.  
  136. /// <summary>
  137. /// 上传图片,不缩放,并添加文字水印
  138. /// </summary>
  139. /// <param name="postedfile">HTTPPOSTEDFILE</param>
  140. /// <param name="uploadpath">保存的全路径,包括文件名</param>
  141. /// <param name="text">水印文字</param>
  142. /// <param name="waterTextFont">文字水印字体</param>
  143. public static void UploadImageWithWaterText(HttpPostedFile postedfile, string uploadpath, string text, Font waterTextFont)
  144. {
  145. Image img = Image.FromStream(postedfile.InputStream);
  146. ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, text, "", waterTextFont);
  147. }
  148.  
  149. /// <summary>
  150. /// 上传图片,不缩放,并添加文字水印
  151. /// </summary>
  152. /// <param name="postedfile">HTTPPOSTEDFILE</param>
  153. /// <param name="uploadpath">保存的全路径,包括文件名</param>
  154. /// <param name="text">水印文字</param>
  155. public static void UploadImageWithWaterText(HttpPostedFile postedfile, string uploadpath, string text)
  156. {
  157. Image img = Image.FromStream(postedfile.InputStream);
  158. ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, text, "", null);
  159. }
  160.  
  161. #endregion 上传图片,不缩放,并添加文字水印
  162.  
  163. #region 上传图片,不缩放,并添加图片水印
  164.  
  165. /// <summary>
  166. /// 上传图片,不缩放,并添加图片水印
  167. /// </summary>
  168. /// <param name="postedfile">源图</param>
  169. /// <param name="uploadpath">保存的路径,包含上传后的文件名</param>
  170. /// <param name="waterimg">水印图片的虚拟路径</param>
  171. public static void UploadImageWithWaterImage(HttpPostedFile postedfile, string uploadpath, string waterimg)
  172. {
  173. Image img = Image.FromStream(postedfile.InputStream);
  174. waterimg = HttpContext.Current.Server.MapPath(waterimg);
  175. ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, "", waterimg, null);
  176. }
  177.  
  178. #endregion 上传图片,不缩放,并添加图片水印
  179.  
  180. /// <summary>
  181. /// 图片等比缩放
  182. /// </summary>
  183. /// <param name="postfile">源图</param>
  184. /// <param name="uploadpath">保存路径及文件名</param>
  185. /// <param name="width">宽度</param>
  186. /// <param name="height">高度</param>
  187. public static void CutImageAutoZoom(HttpPostedFile postfile, string uploadpath, int width, int height)
  188. {
  189. ImageHelper.ZoomAuto(postfile, uploadpath, width, height, "", "", null);
  190. }
  191.  
  192. #endregion 水印
  193.  
  194. private static byte[] GetPostFileByte(HttpPostedFile postfile)
  195. {
  196. int filelength = postfile.ContentLength;
  197. byte[] buffer = new byte[filelength];
  198. postfile.InputStream.Read(buffer, , filelength);
  199. return buffer;
  200. }
  201.  
  202. private static string UploadImageForBMP(HttpPostedFile postfile, string uploadpath)
  203. {
  204. byte[] buffer = GetPostFileByte(postfile);
  205. return UploadImage(buffer, uploadpath, ImageFormat.Bmp);
  206. }
  207.  
  208. private static string UploadImageForGIF(HttpPostedFile postfile, string uploadpath)
  209. {
  210. byte[] buffer = GetPostFileByte(postfile);
  211. return UploadImage(buffer, uploadpath, ImageFormat.Gif);
  212. }
  213.  
  214. private static string UploadImageForJPEG(HttpPostedFile postfile, string uploadpath)
  215. {
  216. byte[] buffer = GetPostFileByte(postfile);
  217. return UploadImage(buffer, uploadpath, ImageFormat.Jpeg);
  218. }
  219.  
  220. private static string UploadImageForPNG(HttpPostedFile postfile, string uploadpath)
  221. {
  222. byte[] buffer = GetPostFileByte(postfile);
  223. return UploadImage(buffer, uploadpath, ImageFormat.Png);
  224. }
  225.  
  226. #endregion 上传图片
  227.  
  228. #region 上传任何文件
  229.  
  230. /// <summary>
  231. /// 上传文件
  232. /// </summary>
  233. /// <param name="postfile">上传的原始文件</param>
  234. /// <param name="uploadpath">保存地址,如:'/upload/images/aaaa.jpg'</param>
  235. /// <returns>返回上传后的文件名</returns>
  236. public static string UploadFile(HttpPostedFile postfile, string uploadpath)
  237. {
  238. try
  239. {
  240. string savepath = HttpContext.Current.Server.MapPath(uploadpath);
  241. if (!Directory.Exists(uploadpath))
  242. Directory.CreateDirectory(uploadpath);
  243.  
  244. string ext = Path.GetExtension(postfile.FileName);
  245. string filename = StringHelper.CreateIDCode() + ext;
  246. if (uploadpath.IndexOf(ext) == -) //判断
  247. {
  248. savepath = savepath + filename;
  249. }
  250. postfile.SaveAs(savepath);
  251. return uploadpath + filename;
  252. }
  253. catch (Exception ex)
  254. {
  255. return ex.Message;
  256. }
  257. }
  258.  
  259. #endregion 上传任何文件
  260. }

C# Upload的更多相关文章

  1. 解决ngnix服务器上的Discuz!x2.5 Upload Error:413错误

    1.修改php.ini sudo nano /etc/php5/fpm/php.ini #打开php.ini找到并修改以下的参数,目的是修改上传限制 max_execution_time = 900 ...

  2. 页面无刷新Upload File

    页面无刷新Upload File. 利用jquery.form.js的ajaxForm提交文件. 具体参考以下代码: 前台html <%@ Page Language="C#" ...

  3. 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?

    复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...

  4. 多文档上传(upload multiple documents)功能不能使用怎么办?

    问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...

  5. web 前端常用组件【06】Upload 控件

    因为有万恶的IE存在,所以当Web项目初始化并进入开发阶段时. 如果是项目经理,需要知道客户将会用什么浏览器来访问系统. 明确知道限定浏览器的情况下,你才能从容的让手下的封装必要的前端组件. 本篇文章 ...

  6. AzCopy Upload Files

    We can use many ways upload our Files to Azure, Than I  Introduction to you a good way, AzCopy ! 1. ...

  7. upload&&download

    package am.demo;  import java.io.File;  import java.io.IOException;  import java.util.Iterator;  imp ...

  8. jQuery File Upload 单页面多实例的实现

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

  9. jQuery File Upload done函数没有返回

    最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...

  10. 富文本编辑器TInyMCE,本地图片上传(Image Upload)

    TinyMCE 官网 (类似:百度的富文本web编辑器UEditor) 第一步 下载 TinyMCE,解压后放入工程,在需要的HTML页面引入tinymce.min.js. 第二步 下载tinyMCE ...

随机推荐

  1. numpy 与 pandas

    numpy: import numpy as np np.array([1,2,3]) 创建数组 np.arange(10).reshape(2,5) 类似于range(起始,终止,步长),可以加re ...

  2. MAKEWORD 宏(macro)

    先看看Microsoft给出的关于MAKEWORD的参考: 从Microsoft给出的参考可以得知,宏MAKEWORD的作用是用于创建一个由bHigh和bLow组成的WORD类型的值. 其中bLow是 ...

  3. Mysql 5.7 密码策略 ERROR 1819 (HY000)

    Mysql 5.7 默认对用户密码有密码强度要求,如果指定弱密码,会提示如下: ERROR (HY000): Your password does not satisfy the current po ...

  4. openssl中RSA数字签名的使用

    参考: OpenSSL命令行工具验证数字签名 客户端需要生成一对密钥,服务器需要生成一对密钥,分别记为client_private.pem/client_public.pem和server_priva ...

  5. 滴水穿石-10GUI

    GUI 图形用户界面 1 Frame 窗体 package d10; //第一导入包 import java.awt.Frame; import java.awt.event.WindowAdapte ...

  6. 20165206 2017-2018-2 《Java程序设计》第七周学习总结

    20165206 2017-2018-2 <Java程序设计>第七周学习总结 教材学习内容总结 MySqL:是世界上最流行的开源数据管理系统. 配置启动MySQL. 连接数据库:Conne ...

  7. DOM对象,控制HTML元素

    认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 节点属性: 遍历 ...

  8. maven依赖jar导出消失问题

      问题:maven依赖jar导出消失问题 新创新的Maven管理的项目,使用的模板是maven-archetype-quickstart,设置maven管理的jar导出时,如下 在每次”update ...

  9. cuda by example【读书笔记2】

    常量内存 用常量内存来替换全局内存可以有效的减少内存带宽 __constant__修饰符标识常量内存,从主机内存复制到GPU上的常量内存时,需要特殊版本的cudaMemcpy(): cudaMemcp ...

  10. mysql四大特性与四种隔离级别

    四大特性 1:原子性.事务是一个不可分割的整体,事务开始的操作,要么全部执行,要么全部不执行. 2:隔离性.同一时间,只允许一个事务请求同一组数据.不同的事务彼此之间没有干扰. 3:一致性.事务开始前 ...