自己写了一个上传类,实现了常用的功能

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.IO;
using  System.Drawing;

/// <summary>
/// UploadFile 的摘要说明
/// </summary>

public   class  UploadFile
{
    
public UploadFile()
    
{
        
//检查根目录,如果没有Temp,TempReadyImg,TempReadyImg1,那么就创建它


        
string strPath = HttpContext.Current.Server.MapPath("~/");

        
if (!File.Exists(strPath + "Temp"))
        
{
            FileStream  fs 
= File.Create(strPath + "Temp");
            
//File.OpenRead(strPath + "Temp");
            fs.Close();
            fs.Dispose();
        }

        
if (!File.Exists(strPath + "TempReadyImg"))
        
{
            FileStream fs 
= File.Create(strPath + "TempReadyImg");
            
//File.OpenRead(strPath + "TempReadyImg");
            fs.Close();
            fs.Dispose();
        }

        
if (!File.Exists(strPath + "TempReadyImg1"))
        
{
            FileStream fs 
= File.Create(strPath + "TempReadyImg1");
            
//File.OpenRead(strPath + "TempReadyImg1");
            fs.Close();
            fs.Dispose();
        }

    }


    
/// <summary>
    
/// 获得文件的二进制数据流
    
/// </summary>
    
/// <param name="filePathFile">需要压制的文件路径和名称全字符串</param>
    
/// <returns>文件二进制流的指针</returns>

    public byte[] GetTempReadyImgData(string filePathName)
    
{
        FileStream fs 
= new FileStream(filePathName, FileMode.Open);
        
int nLength = (int)fs.Length;
        
byte[] fileData = new byte[nLength];
        fs.Read(fileData, 
0, nLength);
        fs.Close();
        
return fileData;
    }


     
/// <summary>
    
/// 绑定上传图片到临时文件 
    
/// </summary>
    
/// <param name="fileUpload">上传图片的控件</param>
    
/// <param name="imgPhoto">显示图片的Image控件的引用</param>
    
/// <param name="clientScriptManager">当前页的客户端脚本管理器</param>
    
/// <return>返回产生的临时文件的文件名(包括路径)</return>

    public string BindUploadPhotoToImage(FileUpload fileUpload, ref System.Web.UI.WebControls.Image imgPhoto, string folder, ClientScriptManager clientScriptManager)
    
{
        
string PathFilename = string.Empty;
        
if (fileUpload.HasFile)
        
{
            
string Folder = folder;
            PathFilename 
= UploadPhotoFile(fileUpload, Folder, clientScriptManager);
        }

        
else
        
{                   
            
if (!clientScriptManager.IsStartupScriptRegistered("NoFile"))
                clientScriptManager.RegisterStartupScript(GetType(), 
"NoFile""alert('请指定上传的文件!')"true);
        }

        imgPhoto.ImageUrl
=PathFilename;

        
return PathFilename;
    }


    
/// <summary>
    
/// 绑定上传图片并且保存为临时文件 
    
/// </summary>
    
/// <param name="fileUpload">上传图片的控件</param>
    
/// <param name="imgPhoto">显示图片的Image控件的引用</param>
    
/// <param name="clientScriptManager">当前页的客户端脚本管理器</param>
    
/// <return>返回产生的临时文件的文件名(包括路径)</return>

    public string BindUploadPhotoToImage(FileUpload fileUpload, ref System.Web.UI.WebControls.Image imgPhoto, ClientScriptManager clientScriptManager)
    
{
        
string Folder = @"../TempImg/";
        
string PathFilename = BindUploadPhotoToImage(fileUpload, ref imgPhoto, Folder ,clientScriptManager);

        
return PathFilename;
    }


    
/// <summary>
    
/// 将文件上传到指定的临时文件夹并产生一个临时文件
    
/// </summary>
    
/// <param name="fileUpload">上传控件的实例</param>
    
/// <param name="folder">上传的文件夹</param>
    
/// <param name="clientScriptManager">当前页的客户端脚本管理器</param>
    
/// <returns>返回上传图片的临时文件名称(包括路径)</returns>

    public string UploadPhotoFile(FileUpload fileUpload, string folder, ClientScriptManager clientScriptManager)
    
{
        FileUpload FileUpload 
= fileUpload;
        
string Filename = FileUpload.FileName;
        
string Folder = folder;

        
string strFileName = string.Empty;
        
string strPath = string.Empty;
        
if (ValidatePhotoFileType(Filename) == true)
        
{
            
try
            
{
                strFileName 
= Guid.NewGuid().ToString();
                strPath 
= HttpContext.Current.Server.MapPath(Folder);
                FileUpload.PostedFile.SaveAs(strPath 
+ strFileName);
            }

            
catch
            
{
                
if (!clientScriptManager.IsStartupScriptRegistered("UploadFail"))
                    clientScriptManager.RegisterStartupScript(
this.GetType(), "UploadFail""alert('文件上传失败!')"true);
            }

        }

        
else
        
{
            
if (!clientScriptManager.IsStartupScriptRegistered("TypeError"))
                clientScriptManager.RegisterStartupScript(
this.GetType(), "TypeError""alert('只允许上传gif,png,jpeg,jpg文件!')"true);
        }


        
return strPath + strFileName;
    }


    
/// <summary>
    
/// 将文件上传到指定的临时文件夹并产生一个临时文件
    
/// </summary>
    
/// <param name="fileUpload">上传控件的实例</param>
    
/// <param name="folder">上传的文件夹</param>
    
/// <param name="allowedExtentions">允许上传的类型</param>
    
/// <param name="clientScriptManager">当前页的客户端脚本管理器</param>
    
/// <returns>返回上传图片的临时文件名称(包括路径)</returns>

    public string UploadTypedFile(FileUpload fileUpload, string folder, string[] allowedExtentions, ClientScriptManager clientScriptManager)
    
{
        FileUpload FileUpload 
= fileUpload;
        
string Filename = FileUpload.FileName;
        
string Folder = folder;
        
string[] AllowedExtentions = allowedExtentions;
        
string strFileName = string.Empty;
        
string strPath = string.Empty;
        
if (ValidateFileType(Filename, AllowedExtentions) == true)
        
{
            
try
            
{
                strFileName 
= Guid.NewGuid().ToString();
                strPath 
= HttpContext.Current.Server.MapPath(Folder);
                FileUpload.PostedFile.SaveAs(strPath 
+ strFileName);
            }

            
catch
            
{
                
if (!clientScriptManager.IsStartupScriptRegistered("UploadFail"))
                    clientScriptManager.RegisterStartupScript(
this.GetType(), "UploadFail""alert('文件上传失败!')"true);
            }

        }

        
else
        
{
            
string FileTypes = string.Empty;
            
int count = AllowedExtentions.Length;
            
if (count == 0)
            
{
                
if (!clientScriptManager.IsStartupScriptRegistered("TypeError"))
                    clientScriptManager.RegisterStartupScript(
this.GetType(), "TypeError""alert('请指定上传的类型!')"true);
            }

            
else
            
{
                
for (int i = 0; i < count; i++)
                
{
                    FileTypes 
= FileTypes + AllowedExtentions[i];
                }

                
if (!clientScriptManager.IsStartupScriptRegistered("TypeError"))
                    clientScriptManager.RegisterStartupScript(
this.GetType(), "TypeError""alert('只允许上传'" + FileTypes + "'文件!')"true);
            }

        }


        
return strPath + strFileName;
    }


    
/// <summary>
    
/// 判断上传的文件类型
    
/// </summary>
    
/// <param name="filename">需要判断的文件名称</param>
    
/// <param name="allowedExtentions">允许的文件后缀名</param>
    
/// <returns>1表示正确, 0表示错误</returns>

    public bool ValidateFileType(string filename, string[] allowedExtentions)
    
{
        
string Filename = filename;
        
//---判断文件名是否正确---
        bool isCorrect = false;
        String fileExtension 
= System.IO.Path.GetExtension(Filename).ToLower();
        String[] AllowedExtensions 
=  allowedExtentions;
        
for (int i = 0; i < AllowedExtensions.Length; i++)
        
{
            
if (fileExtension == AllowedExtensions[i])
            
{
                isCorrect 
= true;
            }

        }

        
return isCorrect;
    }


    
/// <summary>
    
/// 判断上传的是否图片类型的文件
    
/// </summary>
    
/// <param name="filename">需要判断的文件名称</param>
    
/// <returns>1表示正确, 0表示错误</returns>

    public bool ValidatePhotoFileType(string filename)
    
{
        
string Filename = filename;
        String[] AllowedExtensions 
= ".gif"".png"".jpeg"".jpg" };
        
bool isCorrect = ValidateFileType(Filename, AllowedExtensions);
        
return isCorrect;
    }


    
/// <summary>
    
/// 删除已经存在的文件
    
/// </summary>
    
/// <param name="filename">文件的文件名和绝对路径</param>
    
/// <returns>1-删除成功, 0-删除失败</returns>

    public bool DeleteExistFile(string filename)
    
{
        
bool isDelete = false;
        
if (File.Exists(filename))
        
{
            
try
            
{
                File.Delete(filename);
                isDelete 
= true;
            }

            
catch
            
{
                isDelete 
= false;
            }

        }

        
else
        
{
            isDelete 
= false;
        }


        
return isDelete;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值