FileUp(文件上传类)

本文介绍了一个用于处理文件上传和转换的C#类。该类包括将文件转换为字节数组、验证上传文件并将其保存的功能。同时,还提供了从字节数组还原文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;

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();
        }
    }
}

转载于:https://www.cnblogs.com/mynameltg/p/4043465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值