C#文件上传类,文件流,字节数组等
using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
namespace DotNet.Utilities
{
/// <summary>
/// 文件上传类
/// </summary>
public class FileUp
{
public FileUp()
{ }
/// <summary>
/// 转换为字节数组
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>字节数组</returns>
public byte[] GetBinaryFile(string filename)
{
if (File.Exists(filename))
{
FileStream Fsm = null;
try
{
Fsm = File.OpenRead(filename);
return this.ConvertStreamToByteBuffer(Fsm);
}
catch
{
return new byte[0];
}
finally
{
Fsm.Close();
}
}
else
{
return new byte[0];
}
}
/// <summary>
/// 流转化为字节数组
/// </summary>
/// <param name="theStream">流</param>
/// <returns>字节数组</returns>
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int bi;
MemoryStream tempStream = new System.IO.MemoryStream();
try
{
while ((bi = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)bi));
}
return tempStream.ToArray();
}
catch
{
return new byte[0];
}
finally
{
tempStream.Close();
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="PosPhotoUpload">控件</param>
/// <param name="saveFileName">保存的文件名</param>
/// <param name="imagePath">保存的文件路径</param>
public string FileSc(FileUpload PosPhotoUpload, string saveFileName, string imagePath)
{
string state = "";
if (PosPhotoUpload.HasFile)
{
if (PosPhotoUpload.PostedFile.ContentLength / 1024 < 10240)
{
string MimeType = PosPhotoUpload.PostedFile.ContentType;
if (String.Equals(MimeType, "image/gif") || String.Equals(MimeType, "image/pjpeg"))
{
string extFileString = System.IO.Path.GetExtension(PosPhotoUpload.PostedFile.FileName);
PosPhotoUpload.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(imagePath));
}
else
{
state = "上传文件类型不正确";
}
}
else
{
state = "上传文件不能大于10M";
}
}
else
{
state = "没有上传文件";
}
return state;
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="binData">字节数组</param>
/// <param name="fileName">文件名</param>
/// <param name="fileType">文件类型</param>
//-------------------调用----------------------
//byte[] by = GetBinaryFile("E:\\Hello.txt");
//this.SaveFile(by,"Hello",".txt");
//---------------------------------------------
public void SaveFile(byte[] binData, string fileName, string fileType)
{
FileStream fileStream = null;
MemoryStream m = new MemoryStream(binData);
try
{
string savePath = HttpContext.Current.Server.MapPath("~/File/");
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
string File = savePath + fileName + fileType;
fileStream = new FileStream(File, FileMode.Create);
m.WriteTo(fileStream);
}
finally
{
m.Close();
fileStream.Close();
}
}
}
}
C#文件上传类,文件流,字节数组等的更多相关文章
- [上传下载] C#FileUp文件上传类 (转载)
点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...
- php 文件上传类 实例分享
最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...
- php 文件上传类,功能相当齐全,留作开发中备用吧。
收藏一个经典好用的php 文件上传类,功能相当齐全,留作开发中备用吧. 好东西,大家都喜欢,才是真的好,哈哈!!! <?php /** * 文件上传类 */ class upload ...
- PHP实现的多文件上传类及用法示例
这篇文章主要介绍了PHP实现的多文件上传类及用法,详细分析了php实现的多文件上传类与具体的使用技巧,需要的朋友可以参考下 1.upFiles.css.php 文件 <?php class Up ...
- 一个经典的PHP文件上传类
需求分析如下: (1)支持单个文件上传 (2)支持多个文件上传 (3)可以指定保存位置 可以设置上传文件允许的大小和类型 可以选择重命名和保留原名 <!-- 设计一个经典文件上传类 需求分析 ( ...
- ASP.NET 文件上传类 简单好用
调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...
- PHP 文件上传类
FileUpload.; $]; $_newname = date(,). : To ...
- 封装upload文件上传类
<?php //封装php中的单文件(图片)上传类 /* //参数1:$file 文件数组 5个属性值 name,type,size,tmp,error //参数2:文件保存的路径$pa ...
- ThinkPHP文件上传类
TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...
随机推荐
- 蓝桥网试题 java 基础练习 特殊的数字
-------------------------------------------------------- 笑脸 :-) ------------------------------------ ...
- js阻止表单提交的两种方法
下面直接看代码. <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- class path resource [config.xml] cannot be opened because it does not exist
初学Spring在用Resource rs=new ClassPathResource("applicationContext.xml");时老是遇到这个错误.后来发现用Appli ...
- TypeScript入门 2--代码调试
代码调试(debug)是日常开发中必不可少的手段之一,无法进行代码调试会让我们痛苦不已,本文主要介绍如何调试TypeScript代码 很多刚接触TypeScript的人或许有疑问,我们编写的TypeS ...
- Monkey实例测试
三.Monkey实例测试 Windows下(注:2-4步是为了查看我们可以测试哪些应用程序包,可省略): 1. 通过eclipse启动一个Android的emulator或者2 2. 在命令行中输入: ...
- 5_jQuery选择器
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/stric ...
- BZOJ 3925: [Zjoi2015]地震后的幻想乡(概率)
CLJ就是喜欢出ctsc上讲的东西,看来还是得找时间把他的那几道题做下 首先记f(x)为答案>x的概率,那么把这个东西从0到1积分就是答案了 f(x)<=>边小于x不能使图联通的概率 ...
- 从SHAttered事件谈安全
大新闻? 在刚刚过去的2017年2月23日,Cryptology Group at Centrum Wiskunde & Informatica (CWI)和Google的研究人员公开了2个P ...
- Angular.js之内置过滤器学习笔记
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- windows 2003装.net 4.0时提示 WIC windows Imaging Component
运行此安装程序之前,必须安装32位windows映像处理组件(WIC) WIC windows Imaging Component下载地址: http://download.microsoft.com ...