zoukankan      html  css  js  c++  java
  • 文件上传帮助类

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web.UI.WebControls;
    using System.IO;
    
    namespace AIMSCommon
    {
        /// <summary>
        /// 文件上传帮助类
        /// </summary>
        public class UploadHelper
        {
            /// <summary>
            /// 文件上传
            /// </summary>
            /// <param name="path">保存路径</param>
            /// <param name="filleupload">上传文件控件</param>
            /// <returns></returns>
            public static string FileUpload(string path, FileUpload filleupload)
            {
                try
                {
                    bool fileOk = false;
                    //取得文件的扩展名,并转换成小写
                    string fileExtension = System.IO.Path.GetExtension(filleupload.FileName).ToLower();
                    //文件格式
                    string[] allowExtension = { ".rar", ".zip", ".rar", ".ios", ".jpg", ".png", "bmp", ".gif", ".txt" };
                    if (filleupload.HasFile)
                    {
                        //对上传的文件的类型进行一个个匹对
                        for (int i = 0; i < allowExtension.Length; i++)
                        {
                            if (fileExtension == allowExtension[i])
                            {
                                fileOk = true;
                                break;
                            }
                        }
                    }
                    //如果符合条件,则上传
                    if (fileOk)
                    {
                        if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
                        {
                            Directory.CreateDirectory(path);
                        }
                        if (!FileHelper.IsExistFile(path + filleupload.FileName))
                        {
                            int Size = filleupload.PostedFile.ContentLength / 1024 / 1024;
                            if (Size > 10)
                            {
                                return "上传失败,文件过大";
                            }
                            else
                            {
                                filleupload.PostedFile.SaveAs(path + filleupload.FileName);
                                return "上传成功";
                            }
                        }
                        else
                        {
                            return "上传失败,文件已存在";
                        }
    
                    }
                    else
                    {
                        return "不支持【" + fileExtension + "】文件格式";
                    }
                }
                catch (Exception)
                {
                    return "上传失败";
                }
            }
        }
    }
    
  • 相关阅读:
    原生JS之温故而知新(一)
    jQuery版本问题
    AngularJs我的学习整理(不定时修改)
    Js事件处理程序跨浏览器
    AngularJs的关于路由问题
    很棒的十句话,分享给大家。
    一个人为什么要努力?
    春熙路。
    成都
    springboot 使用mybatis-generator自动生成代码
  • 原文地址:https://www.cnblogs.com/zhangxiaolei521/p/5660979.html
Copyright © 2011-2022 走看看