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 ...
随机推荐
- poj2728 生成树01分数规划 (二分答案)
给定整数序列a,b,求出下式的最大值 sum{ai*xi}/sum{bi*xi},xi=0|1 通俗来说,就是选出一些整数对(ai,bi),使得选出的a之和与选出的b之和商最大化 二分答案L,即选出的 ...
- hdu1213并查集
板子题不多说,上代码 #include<iostream> #include<cstdio> #include<cstring> using namespace s ...
- bzoj 3129
非常好的一道数学题,考察了大量数论和组合数学的知识 在做本题之前强烈建议先完成下列两个背景知识: ①: bzoj 2142礼物 因为本题的一部分数据需要利用到拓展卢卡斯定理,而礼物是拓展卢卡斯定理的裸 ...
- centos/redhat破解账号密码
说明:1.个人觉得centos系统和redhat系统差不多,界面都差不多一样. 2.下面方法用于开机root密码忘了,其他人篡改root密码等等 下面是破解账号密码(图解) 之后要等久点 效果: 方法 ...
- html5页面调用手机打电话功能
<head>里面加上:<meta name="format-detection" content="telephone=yes"/> 需 ...
- 20165323 2017-2018-2 《Java程序设计》课程总结
一.每周作业链接汇总 预备作业1:20165323 我期望的师生关系 预备作业2:20165323 学习基础与C语言学习心得 预备作业3:20165323 预备作业三 第一周作业:20165323&l ...
- Java生成生成密码类
import java.util.Date; import java.util.Random; public class PasswordUtil { public final static Stri ...
- TodoMVC:帮助你选择一个MV*框架
开发者现在有很多的MV*框架选择来组织开发web应用程序.Backbone. Ember.AngularJS.Spine… 新的稳定解决方案列表持续增长,但你如何决定在海量的框架中选择哪个使用? 为了 ...
- javascript获取时间戳
时间戳: 时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数.它也被称为 Unix 时间戳(Unix Timestamp). JavaScript 获取当前时间戳: < ...
- 【bzoj4631】踩气球 线段树
题解: 真是很zz 我都想到线段树分治的思路了... 不过还是一道好题 首先跟线段树分治一样将区间投射到线段树上去 每次修改如果该个区间修改为0,则对他们对应的特定区间-1 这样每个区间会有一次变0, ...