首页 > 代码库 > FileUpload 上传文件 帮助类

FileUpload 上传文件 帮助类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//=====================================================================================
// All Rights Reserved , Copyright ? Learun 2013
//=====================================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using DotNet.Utilities;
 
namespace DotNet.Utilities
{
    /// <summary>
    /// 文件上传帮助类
    /// </summary>
    public class UploadHelper
    {
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="path">保存路径</param>
        /// <param name="filleupload">上传文件控件</param>
        /// <param name="filename">文件名</param>
        /// <param name="fileExtension">后缀名</param>
        /// <param name="filesize">文件大小</param>
        /// <returns></returns>
        public static string FileUpload(FileUpload filleupload, string path, out string filename, out string fileExtension, out string filesize)
        {
            string FileName = CommonHelper.GetGuid;
            if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path);
            }
            //取得文件的扩展名,并转换成小写
            string Extension = System.IO.Path.GetExtension(filleupload.FileName).ToLower();
            fileExtension = Extension;
            filename = FileName + Extension;
            //取得文件大小
            filesize = FileHelper.CountSize(filleupload.PostedFile.ContentLength);
            try
            {
                int Size = filleupload.PostedFile.ContentLength / 1024 / 1024;
                if (Size > 10)
                {
                    return "上传失败,文件过大";
                }
                else
                {
                    filleupload.PostedFile.SaveAs(path + filename);
                    return "上传成功";
                }
            }
            catch (Exception)
            {
                return "上传失败";
            }
        }
 
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="path">保存路径</param>
        /// <param name="filleupload">上传文件控件</param>
        /// <param name="filename">文件名ID</param>
        /// <param name="fileExtension">后缀名</param>
        /// <param name="filesize">文件大小</param>
        /// <param name="filesize">文件名</param>
        /// <returns></returns>
        public static string FileUpload(FileUpload filleupload, string path, out string filename_id, out string fileExtension, out string filesize,out string filename)
        {
            string FileName_Id = CommonHelper.GetGuid;
            if (Directory.Exists(path) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(path);
            }
            //取得文件的扩展名,并转换成小写
            string Extension = System.IO.Path.GetExtension(filleupload.FileName).ToLower();
            fileExtension = Extension;
            filename_id = FileName_Id + Extension;
            filename = filleupload.FileName;
            //取得文件大小
            filesize = FileHelper.CountSize(filleupload.PostedFile.ContentLength);
            try
            {
                int Size = filleupload.PostedFile.ContentLength / 1024 / 1024;
                if (Size > 10)
                {
                    return "上传失败,文件过大";
                }
                else
                {
                    filleupload.PostedFile.SaveAs(path + filename);
                    return "上传成功";
                }
            }
            catch (Exception)
            {
                return "上传失败";
            }
        }
 
    }
}