#region 文件上传
public static string UploadVideoFile(HttpPostedFile file, MyVideoEnum videoType = MyVideoEnum.DescFile)
{
if (file == null) { return string.Empty; }
if (file.ContentLength > 0)
{
if (!string.IsNullOrEmpty(file.FileName))
{
string ext = Path.GetExtension(file.FileName).ToLower();//当前文件后缀名
//验证文件类型是否正确
if (ext.Equals(".mp4") || ext.Equals(".avi") || ext.Equals(".asf"))
{
string uploadPath = "/UploadVideo/" + videoType + "/";
string newFilename = GenerateFilename(ext);
//当前待上传的服务端路径相对
var videoUrl = uploadPath + newFilename;
string folder = AppDomain.CurrentDomain.BaseDirectory + uploadPath;
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
var imageFilePath = AppDomain.CurrentDomain.BaseDirectory + videoUrl;
file.SaveAs(imageFilePath);
return videoUrl;
}
else
{
return string.Empty;
}
}
}
return string.Empty;
}
/// <summary>
/// 生成文件名称
/// </summary>
/// <param name="extension"></param>
/// <returns></returns>
public static string GenerateFilename(string extension)
{
return Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture) + extension;
}
public enum MyVideoEnum
{
OtherFile = 0,//其他目录
DescFile = 1, //简介目录
CourseFile = 2,//课程目录
}
#endregion