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的更多相关文章

  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. PHP Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in /usr/local/php/CreateDB.php on line 5

    原因:php还不支持mysql8.0最新的密码加密方式 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ' ...

  2. Vuex状态管理模式的面试题及答案

    转载:点击查看原文 1.vuex有哪几种属性? 答:有五种,分别是 State. Getter.Mutation .Action. Module 2.vuex的State特性是? 答: 一.Vuex就 ...

  3. mysql下载源码方法

    方法一 进入mysql官网:http://dev.mysql.com/downloads/mysql/ 选择相关的平台下载: 3.选择Source Code 选型后,拉倒网页下方,选择要下载的源码包 ...

  4. mysql 查看某个数据库中所有表的数据量

    1.登录mysql 2.使用命令:use information_schema; 3.使用命令:select table_name,table_rows from tables where TABLE ...

  5. 使用sysbench 0.5 对mysql 进行性能、压力测试

    sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.目前sysbench代码托管在launchpad上,项目地址:https://launc ...

  6. Git推送错误Remote: User permission denied错误解决方法

    用了别的同事的电脑,推送代码,报错. 解决方法: 修改别人的密码,改成自己的账号和密码就可以了.

  7. Haproxy重刷一次

    centos上,yum安装,完全无难度. 只是设置时,要注意一下跳转,和nginx规则差不多. https://blog.csdn.net/qq_28710983/article/details/82 ...

  8. Kafka的生产者和消费者代码解析

    :Kafka名词解释和工作方式 1.1:Producer :消息生产者,就是向kafka broker发消息的客户端. 1.2:Consumer :消息消费者,向kafka broker取消息的客户端 ...

  9. 利用 Windows API Code Pack 修改音乐的 ID3 信息

    朋友由于抠门 SD 卡买小了,结果音乐太多放不下,又不舍得再买新卡,不得已决定重新转码,把音乐码率压低一点,牺牲点音质来换空间(用某些人的话说,反正不是搞音乐的,听不出差别)… 结果千千静听(百度音乐 ...

  10. KnocoutJs+Mvc+BootStrap 学习笔记(Mvc)

    Mvc   1.Html 增加扩展方法 using System.Web.Mvc; namespace KnockoutBootstrapMvc.Entensions { public static ...