檔案上傳協助類

來源:互聯網
上載者:User

標籤:

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 "上傳失敗";            }        }    }}

 

檔案上傳協助類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.