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 ...
随机推荐
- PhpStorm 2018 安装及破解方法
参考教程: https://blog.csdn.net/u012278016/article/details/81772566
- select前台转义后台取到的值为对应的文本 select同时接受list和map
简单描述:select动态取值 要求是根据后台传过来的值在前台进行转义,emmm干就完了 思路分析:后台同时传过去一个map一个list ,map用来前台转义,list用来获取值,list取到的值相当 ...
- laravel 更新
public function update(Request $request, ResponseFactoryContract $response) { $user = $request->u ...
- cf919D 线性dp+拓扑排序
/* 给定一张有向图,图上每个结点都有一个字符,现在要求出一条路径,要使路径上某字符出现的次数最多 如果有环,输出-1即可 拓扑排序+dp dp[i][26]表示排序到结点i时26个字符出现的次数 在 ...
- Python中的xxx == xx是否等价于xxx is xxx
先上代码: >>> a = 1 >>> b = 1 >>> a is b True >>> a == b True what? ...
- C++ Primer 笔记——类成员指针
1.当我们初始化一个成员指针或为成员指针赋值时,该指针并没有指向任何数据.成员指针指定了成员而非成员所属的对象,只有当解引用成员指针时,我们才提供对象信息. 2.和普通的函数指针类似,如果成员存在重载 ...
- Spring Cloud Eureka简介及原理
Eureka是Netflix开发的服务发现组件,本身是一个基于REST的服务.Spring Cloud将它集成在其子项目spring-cloud-netflix中,以实现Spring Cloud的服务 ...
- WCF寄宿IIS
1.创建一个简单的wcf项目 创建完成后直接运行,结果 然后进行发布 在IIS上新建一个网站,直接进行发布即可 遇到的问题 请求与通配符 mime 映射相匹配.请求映射到静态文件处理程序. 需要注意的 ...
- VMware搭建虚拟机服务器
一.需求点描述: 1.在有路由器的情况下,能够通过固定的外网IP访问路由器中某台实体机中运行的虚拟机. 2.能够通过外网IP访问该虚拟机中的ftp.远程连接.iis.tomcat等. 二.原理分析: ...
- 一脸懵逼学习oracle
oracle的默认用户:system,sys,scott: 1:查看登录的用户名:show user: 2:查看数据字典:dba_users; 3:创建新用户 (1)要连接到Oracle数据库,就需要 ...