代码改变世界

视频上传

2009-09-03 11:32  smat  阅读(348)  评论(0编辑  收藏  举报
protected void btnUpload_Click(object sender, EventArgs e)
    {
       
//判断是否上传了文件
        if (fileUpload.HasFile)
        {
           
//指定上传文件在服务器上的保存路径
            string savePath = Server.MapPath("~/upload/");
           
//检查服务器上是否存在这个物理路径,如果不存在则创建
            if (!System.IO.Directory.Exists(savePath))
            {
               
//需要注意的是,需要对这个物理路径有足够的权限,否则会报错
               
//另外,这个路径应该是在网站之下,而将网站部署在C盘却把上传文件保存在D盘
                System.IO.Directory.CreateDirectory(savePath);
            }
            savePath
= savePath + "\\" + fileUpload.FileName;
            fileUpload.SaveAs(savePath);
//保存文件
           
//不过需要注意的是,在客户端访问却需要指定的是URL地址,而不是在服务器上的物理地址
            literal.Text = string.Format("<a href='upload/{0}'>upload/{0}</a>", fileUpload.FileName);
        }
    }
或:
public partial class admin_UpSoft : System.Web.UI.Page
{
    CAN can
= new CAN();
   
protected void Page_Load(object sender, EventArgs e)
    {

    }
   
protected void btnUpdate_Click(object sender, EventArgs e)
    {
       
string filepath = "", fileExtName = "", mFileName,mPath;
       
if ("" != this.File2.PostedFile.FileName)
        {
         filepath
=File2 .PostedFile.FileName;
         fileExtName
= filepath.Substring(filepath.LastIndexOf(".")+1);
           
try
            {
             mPath
=Server.MapPath("..\\FileDown\\");
                mFileName
=filepath .Substring(filepath.LastIndexOf("\\")+1);
                File2.PostedFile .SaveAs(mPath
+mFileName);
                TextBox2 .Text
="FileDown\\"+mFileName;
                File2.Visible
=false;
                TextBox2.Visible
=true;
            }
           
catch(Exception err)
            {
                Response.Write(err.ToString());
           
            }
        }
        can.ExecSqlCom(
"Insert into Soft(SoftName,DownLoad) values('" + TextBox1.Text + "','" + TextBox2.Text + "')");
        Response.Write(
"<script language=javascript>alert('上传成功!');location='javascript:history.go(-1)'</script>");
    }
   
protected void Button1_Click(object sender, EventArgs e)
    {
        Page.Response.Redirect(
"main.aspx");

    } 

上传图片: 

 

private string UploadPic()
        {
            
if(txtPictureUploader.PostedFile.FileName.Length ==0)
            {
                
return string.Empty ;
            }
                
            
string fileName = Guid.NewGuid().ToString();
            
int position = txtPictureUploader.PostedFile.FileName.LastIndexOf(".");
            
if(position == -1)
            {
                
throw new Exception("上传的文件格式不正确!");
            }
            
string extensionName = txtPictureUploader.PostedFile.FileName.Substring(position).ToLower();
            
if(extensionName == ".png"||extensionName == ".jpg"||extensionName == ".gif"||extensionName == ".jpeg"||extensionName == ".bmp")
            {
                
string filePath=ConfigurationSettings.AppSettings["UploadVideoImagePath"];
                fileName 
= fileName+extensionName;
                
if(filePath==null)
                    filePath 
="/Images/";
            
                txtPictureUploader.PostedFile .SaveAs(Server.MapPath( filePath)
+ fileName) ;
                
return fileName;
            }
            
else
            {
                
throw new Exception("上传的文件格式不正确!");
            }
        }

 

 上传视频:  

 

private string UploadVideo()
        {
            
if(txtVideoFileUploader.FileName.Length ==0)
            {
                
return string.Empty ;
            }
                
            
string fileName = Guid.NewGuid().ToString();
            
int position = txtVideoFileUploader.FileName.LastIndexOf(".");
            
if(position == -1)
            {
                
throw new Exception("上传的文件格式不正确!");
            }
            
string extensionName = txtVideoFileUploader.FileName.Substring(position);
            
string filePath=ConfigurationSettings.AppSettings["UploadVideosPath"];
            fileName 
= fileName+extensionName;
            
if(filePath==null)
                filePath 
="/Videos/";
            txtVideoFileUploader.MoveTo(Server.MapPath(filePath
+fileName),MoveToOptions.Overwrite);
            
return fileName;

}