C# Upload
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Web;
- using Bo_myCommon;
- public class Upload
- {
- #region 上传图片
- /// <summary>
- /// 上传图片
- /// </summary>
- /// <param name="imgBuffer">字节数组</param>
- /// <param name="uploadpath">保存路径。绝对或虚拟路径</param>
- /// <param name="imgformat">图片保存格式</param>
- /// <returns>上传成功后返回的新的文件名</returns>
- public static string UploadImage(byte[] imgBuffer, string uploadpath, ImageFormat imgformat)
- {
- try
- {
- System.IO.MemoryStream m = new MemoryStream(imgBuffer);
- if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath)))
- Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath));
- string imgname = StringHelper.CreateIDCode() + "." + imgformat.ToString().ToLower();
- string _path = HttpContext.Current.Server.MapPath(uploadpath) + imgname;
- Image img = System.Drawing.Image.FromStream(m);
- img.Save(_path, imgformat);
- m.Close();
- return uploadpath + imgname;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- /// <summary>
- /// 上传图片
- /// </summary>
- /// <param name="stream">Stream</param>
- /// <param name="uploadpath">保存路径。绝对或虚拟路径</param>
- /// <param name="imgformat">图片保存格式</param>
- /// <returns>上传成功后返回的新的文件名</returns>
- public static string UploadImage(Stream stream, string uploadpath, ImageFormat imgformat)
- {
- try
- {
- Image img = Image.FromStream(stream);
- string filename = StringHelper.CreateIDCode() + "." + imgformat.ToString().ToLower();
- filename = HttpContext.Current.Server.MapPath(uploadpath) + filename;
- img.Save(filename, imgformat);
- return filename;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- /// <summary>
- /// 上传图片
- /// </summary>
- /// <param name="postfile">客户端上传的文件</param>
- /// <param name="uploadpath">保存地址</param>
- /// <param name="imgformat">图片格式</param>
- /// <returns></returns>
- public static string UploadImage(HttpPostedFile postfile, string uploadpath, ImageFormat imgformat)
- {
- switch (imgformat.ToString().ToLower())
- {
- case "jpeg":
- return UploadImageForJPEG(postfile, uploadpath);
- case "bmp":
- return UploadImageForBMP(postfile, uploadpath);
- case "png":
- return UploadImageForPNG(postfile, uploadpath);
- case "gif":
- return UploadImageForGIF(postfile, uploadpath);
- default:
- return UploadImageForJPEG(postfile, uploadpath);
- }
- }
- /// <summary>
- /// 上传图片,保存为JPEG格式
- /// </summary>
- /// <param name="postfile">HttpPostedFile</param>
- /// <param name="uploadpath">保存文件地址</param>
- /// <returns>返回上传后的路径</returns>
- public static string UploadImage(HttpPostedFile postfile, string uploadpath, bool autoImageName)
- {
- if (autoImageName)
- {
- switch (Path.GetExtension(postfile.FileName).ToLower())
- {
- case ".jpg":
- return UploadImageForJPEG(postfile, uploadpath);
- case ".gif":
- return UploadImageForGIF(postfile, uploadpath);
- case ".png":
- return UploadImageForPNG(postfile, uploadpath);
- default:
- return UploadImageForJPEG(postfile, uploadpath);
- }
- }
- else
- {
- Image img = Image.FromStream(postfile.InputStream);
- ImageHelper.ZoomAuto(postfile, uploadpath, img.Width, img.Height, "", "", null);
- return uploadpath;
- }
- }
- /// <summary>
- /// 自动生成新的图片名称
- /// </summary>
- /// <param name="postfile"></param>
- /// <param name="uploadpath"></param>
- /// <returns></returns>
- public static string UploadImage(HttpPostedFile postfile, string uploadpath)
- {
- return UploadImage(postfile, uploadpath, true);
- }
- #region 水印
- #region 上传图片,不缩放,并添加文字水印
- /// <summary>
- /// 上传图片,不缩放,并添加文字水印
- /// </summary>
- /// <param name="postedfile">HTTPPOSTEDFILE</param>
- /// <param name="uploadpath">保存的全路径,包括文件名</param>
- /// <param name="text">水印文字</param>
- /// <param name="waterTextFont">文字水印字体</param>
- public static void UploadImageWithWaterText(HttpPostedFile postedfile, string uploadpath, string text, Font waterTextFont)
- {
- Image img = Image.FromStream(postedfile.InputStream);
- ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, text, "", waterTextFont);
- }
- /// <summary>
- /// 上传图片,不缩放,并添加文字水印
- /// </summary>
- /// <param name="postedfile">HTTPPOSTEDFILE</param>
- /// <param name="uploadpath">保存的全路径,包括文件名</param>
- /// <param name="text">水印文字</param>
- public static void UploadImageWithWaterText(HttpPostedFile postedfile, string uploadpath, string text)
- {
- Image img = Image.FromStream(postedfile.InputStream);
- ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, text, "", null);
- }
- #endregion 上传图片,不缩放,并添加文字水印
- #region 上传图片,不缩放,并添加图片水印
- /// <summary>
- /// 上传图片,不缩放,并添加图片水印
- /// </summary>
- /// <param name="postedfile">源图</param>
- /// <param name="uploadpath">保存的路径,包含上传后的文件名</param>
- /// <param name="waterimg">水印图片的虚拟路径</param>
- public static void UploadImageWithWaterImage(HttpPostedFile postedfile, string uploadpath, string waterimg)
- {
- Image img = Image.FromStream(postedfile.InputStream);
- waterimg = HttpContext.Current.Server.MapPath(waterimg);
- ImageHelper.ZoomAuto(postedfile, uploadpath, img.Width, img.Height, "", waterimg, null);
- }
- #endregion 上传图片,不缩放,并添加图片水印
- /// <summary>
- /// 图片等比缩放
- /// </summary>
- /// <param name="postfile">源图</param>
- /// <param name="uploadpath">保存路径及文件名</param>
- /// <param name="width">宽度</param>
- /// <param name="height">高度</param>
- public static void CutImageAutoZoom(HttpPostedFile postfile, string uploadpath, int width, int height)
- {
- ImageHelper.ZoomAuto(postfile, uploadpath, width, height, "", "", null);
- }
- #endregion 水印
- private static byte[] GetPostFileByte(HttpPostedFile postfile)
- {
- int filelength = postfile.ContentLength;
- byte[] buffer = new byte[filelength];
- postfile.InputStream.Read(buffer, , filelength);
- return buffer;
- }
- private static string UploadImageForBMP(HttpPostedFile postfile, string uploadpath)
- {
- byte[] buffer = GetPostFileByte(postfile);
- return UploadImage(buffer, uploadpath, ImageFormat.Bmp);
- }
- private static string UploadImageForGIF(HttpPostedFile postfile, string uploadpath)
- {
- byte[] buffer = GetPostFileByte(postfile);
- return UploadImage(buffer, uploadpath, ImageFormat.Gif);
- }
- private static string UploadImageForJPEG(HttpPostedFile postfile, string uploadpath)
- {
- byte[] buffer = GetPostFileByte(postfile);
- return UploadImage(buffer, uploadpath, ImageFormat.Jpeg);
- }
- private static string UploadImageForPNG(HttpPostedFile postfile, string uploadpath)
- {
- byte[] buffer = GetPostFileByte(postfile);
- return UploadImage(buffer, uploadpath, ImageFormat.Png);
- }
- #endregion 上传图片
- #region 上传任何文件
- /// <summary>
- /// 上传文件
- /// </summary>
- /// <param name="postfile">上传的原始文件</param>
- /// <param name="uploadpath">保存地址,如:'/upload/images/aaaa.jpg'</param>
- /// <returns>返回上传后的文件名</returns>
- public static string UploadFile(HttpPostedFile postfile, string uploadpath)
- {
- try
- {
- string savepath = HttpContext.Current.Server.MapPath(uploadpath);
- if (!Directory.Exists(uploadpath))
- Directory.CreateDirectory(uploadpath);
- string ext = Path.GetExtension(postfile.FileName);
- string filename = StringHelper.CreateIDCode() + ext;
- if (uploadpath.IndexOf(ext) == -) //判断
- {
- savepath = savepath + filename;
- }
- postfile.SaveAs(savepath);
- return uploadpath + filename;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- #endregion 上传任何文件
- }
C# Upload的更多相关文章
- 解决ngnix服务器上的Discuz!x2.5 Upload Error:413错误
1.修改php.ini sudo nano /etc/php5/fpm/php.ini #打开php.ini找到并修改以下的参数,目的是修改上传限制 max_execution_time = 900 ...
- 页面无刷新Upload File
页面无刷新Upload File. 利用jquery.form.js的ajaxForm提交文件. 具体参考以下代码: 前台html <%@ Page Language="C#" ...
- 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?
复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...
- 多文档上传(upload multiple documents)功能不能使用怎么办?
问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...
- web 前端常用组件【06】Upload 控件
因为有万恶的IE存在,所以当Web项目初始化并进入开发阶段时. 如果是项目经理,需要知道客户将会用什么浏览器来访问系统. 明确知道限定浏览器的情况下,你才能从容的让手下的封装必要的前端组件. 本篇文章 ...
- AzCopy Upload Files
We can use many ways upload our Files to Azure, Than I Introduction to you a good way, AzCopy ! 1. ...
- upload&&download
package am.demo; import java.io.File; import java.io.IOException; import java.util.Iterator; imp ...
- jQuery File Upload 单页面多实例的实现
jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...
- jQuery File Upload done函数没有返回
最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...
- 富文本编辑器TInyMCE,本地图片上传(Image Upload)
TinyMCE 官网 (类似:百度的富文本web编辑器UEditor) 第一步 下载 TinyMCE,解压后放入工程,在需要的HTML页面引入tinymce.min.js. 第二步 下载tinyMCE ...
随机推荐
- numpy 与 pandas
numpy: import numpy as np np.array([1,2,3]) 创建数组 np.arange(10).reshape(2,5) 类似于range(起始,终止,步长),可以加re ...
- MAKEWORD 宏(macro)
先看看Microsoft给出的关于MAKEWORD的参考: 从Microsoft给出的参考可以得知,宏MAKEWORD的作用是用于创建一个由bHigh和bLow组成的WORD类型的值. 其中bLow是 ...
- Mysql 5.7 密码策略 ERROR 1819 (HY000)
Mysql 5.7 默认对用户密码有密码强度要求,如果指定弱密码,会提示如下: ERROR (HY000): Your password does not satisfy the current po ...
- openssl中RSA数字签名的使用
参考: OpenSSL命令行工具验证数字签名 客户端需要生成一对密钥,服务器需要生成一对密钥,分别记为client_private.pem/client_public.pem和server_priva ...
- 滴水穿石-10GUI
GUI 图形用户界面 1 Frame 窗体 package d10; //第一导入包 import java.awt.Frame; import java.awt.event.WindowAdapte ...
- 20165206 2017-2018-2 《Java程序设计》第七周学习总结
20165206 2017-2018-2 <Java程序设计>第七周学习总结 教材学习内容总结 MySqL:是世界上最流行的开源数据管理系统. 配置启动MySQL. 连接数据库:Conne ...
- DOM对象,控制HTML元素
认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 节点属性: 遍历 ...
- maven依赖jar导出消失问题
问题:maven依赖jar导出消失问题 新创新的Maven管理的项目,使用的模板是maven-archetype-quickstart,设置maven管理的jar导出时,如下 在每次”update ...
- cuda by example【读书笔记2】
常量内存 用常量内存来替换全局内存可以有效的减少内存带宽 __constant__修饰符标识常量内存,从主机内存复制到GPU上的常量内存时,需要特殊版本的cudaMemcpy(): cudaMemcp ...
- mysql四大特性与四种隔离级别
四大特性 1:原子性.事务是一个不可分割的整体,事务开始的操作,要么全部执行,要么全部不执行. 2:隔离性.同一时间,只允许一个事务请求同一组数据.不同的事务彼此之间没有干扰. 3:一致性.事务开始前 ...