//上传时,判断文件是否大于限定大小
if (this.FileUpload1.PostedFile.ContentLength > 104857600) //单位KB
{
objInsusJsUtility.JsAlert("You select the file larger than 100MB");
return;
}
//创建一个临时文件夹
string tempPath = "~/Temp/";
//判断目录是否存在
if (!Directory.Exists(Server.MapPath(tempPath)))
{
//如果不存在,创建它
Directory.CreateDirectory(Server.MapPath(tempPath));
}
//取得上传文件
string uploadfile = FileUpload1.PostedFile.FileName;
//取得原文件名,存入数据库中,这样在aspx显示回原来的文件名
string oldFileName = uploadfile.Substring(uploadfile.LastIndexOf(@"/") + 1);
//取得文件的扩展名
string fileExtension = uploadfile.Substring(uploadfile.LastIndexOf("."));
//产生新文件名
string newFileName = objGuid.ToString() + fileExtension;
//建立存储的目录
string directory = Mediadirectory + this.ddlMediaType.SelectedItem.Value + "/";
//判断目录是否存在
if (!Directory.Exists(Server.MapPath(directory)))
{
//如果不存在,创建它
Directory.CreateDirectory(Server.MapPath(directory));
}
//新文件
string newFile = Server.MapPath(tempPath + newFileName);
//保存文件(暂存入一个临时文件夹中)
FileUpload1.SaveAs(newFile);
//限定上传的文件类型
string[] fileClass = { "7076", "4838" }; //7076 is FLV;4838 is wmv;
if (!InsusBase.CompareFileClass(newFile, fileClass))
{
objInsusJsUtility.JsAlert("You did not specify a media file.The file format is wmv,flv");
return;
}
try
{
//存入数据库中
objMedia.Insert(this.ddlMediaType.SelectedItem.Value, this.txtSubject.Text.Trim(), this.txtDescription.Text.Trim(), directory, oldFileName, newFileName);
//把文件从临时文件夹中,移至真正的目录。
File.Move(newFile, Server.MapPath(directory + newFileName));
objInsusJsUtility.JsAlert("视频上传成功。", "this", "Media.aspx");
}
catch (Exception ex)
{
//抛出异常
InsusBase.InsusException(ex);
}
Web.config配置可上传大文件,asp.net默认情况之下只能上传4MB,另外一点就是,maxRequestLength单位是MB。
<system.web>
<httpRuntime maxRequestLength="102400" useFullyQualifiedRedirectUrl="true" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="true"/>
</system.web>