C#实现Web文件的上传

using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Configuration;
using System.Drawing;

namespace zhuanti
{
/// <summary>
/// 这是一个用于玩家投稿中实现玩家上传文件功能中用到的相应的函数的功能模块
/// </summary>
public class FileUpload
{
public enum File //定义一个人用于存放玩家上传文件信息的一个数组
{
FILE_SIZE , //大小
FILE_POSTNAME, //类型(文件后缀名)
FILE_SYSNAME , //系统名
FILE_ORGINNAME, //原来的名字
FILE_PATH //文件路径
}
private static Random rnd = new Random(); //获取一个随机数

public static string[] UploadFile(HtmlInputFile file,string Upload_Dir) //实现玩家文件上传功能的主函数
{
string[] arr = new String[5];
string FileName = GetUniquelyString(); //获取一个不重复的文件名
string FileOrginName = file.PostedFile.FileName.Substring

(file.PostedFile.FileName.LastIndexOf("//")+1);//获取文件的原始名
if(file.PostedFile.ContentLength<=0)
{ return null; }
string postFileName;
string FilePath = Upload_Dir.ToString();
string path = FilePath + "//";
try
{
int pos = file.PostedFile.FileName.LastIndexOf(".")+1;
postFileName = file.PostedFile.FileName.Substring(pos,file.PostedFile.FileName.Length-pos);
file.PostedFile.SaveAs(path+FileName+"."+postFileName); //存储指定的文件到指定的目录
}
catch(Exception exec)
{
throw(exec);
}

double unit = 1024;
double size = Math.Round(file.PostedFile.ContentLength/unit,2);
arr[(int)File.FILE_SIZE] = size.ToString(); //文件大小
arr[(int)File.FILE_POSTNAME] = postFileName; //文件类型(文件后缀名)
arr[(int)File.FILE_SYSNAME] = FileName; //文件系统名
arr[(int)File.FILE_ORGINNAME] = FileOrginName; //文件原来的名字
arr[(int)File.FILE_PATH]=path+FileName+"."+postFileName; //文件路径
return arr;
}

public static bool OperateDB(string sqlstr) //建立一个和数据库的关联
{
if (sqlstr==String.Empty)
return false;

SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connstring"]);
SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);

myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
return true;
}

public static string GetUniquelyString() //获取一个不重复的文件名
{
const int RANDOM_MAX_VALUE = 1000;
string strTemp,strYear,strMonth,strDay,strHour,strMinute,strSecond,strMillisecond;

DateTime dt =DateTime.Now;
int rndNumber = rnd.Next(RANDOM_MAX_VALUE);
strYear = dt.Year.ToString ();
strMonth = (dt.Month > 9)? dt.Month.ToString() : "0" + dt.Month.ToString();
strDay = (dt.Day > 9)? dt.Day.ToString() : "0" + dt.Day.ToString();
strHour = (dt.Hour > 9)? dt.Hour.ToString() : "0" + dt.Hour.ToString();
strMinute = (dt.Minute > 9)? dt.Minute.ToString() : "0" + dt.Minute.ToString();
strSecond = (dt.Second > 9)? dt.Second.ToString() : "0" + dt.Second.ToString();
strMillisecond = dt.Millisecond.ToString();

strTemp = strYear + strMonth + strDay +"_"+ strHour + strMinute + strSecond +"_"+ strMillisecond +"_"+ rndNumber.ToString () ;

return strTemp;
}
}
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值