以上传电影为例:  

protected void btnUpFile_Click(object sender, EventArgs e)

    {
        //修改影片信息时,单击上传按钮,删除原来的文件
        string film = Server.MapPath("..\\film\\")+Request["film"];
        if (File.Exists("film"))
        {
            File.Delete(film);
        }
        string filePath = this.file1.PostedFile.FileName;//获得上传文件的路径
        string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);//获得文件名
        string fileExtend = filePath.Substring(filePath.LastIndexOf(".") + 1);//获得扩展名
        if (!(fileExtend == "RM" || fileExtend == "wma" || fileExtend == "mp3" || fileExtend == "rmvb"))//判断是否是指定格式的文件,否则无法进行上传
        {
            Response.Write(baseclass.strJS("格式错误!", "history.back()"));
            Response.End();
        }
        string serverPath = Server.MapPath("..\\film\\") + fileName;//把文件保存到服务器上的路径
        //判断文件是否存在
        if (File.Exists(serverPath))
        {
            Response.Write(baseclass.strJS("对不起,文件已经存在!", "history.back()"));
            Response.End();
        }
        this.file1.PostedFile.SaveAs(serverPath);//保存文件到服务器
        string itemid = Convert.ToString(Session["itemid"]);//获取DataList控件中ItemIndex
        if(itemid.Length<2)
        {
            itemid = "0" + itemid;
        }
        if (Request["addtype"] == "add")//如果是添加信息返回父页面指定的文本框
        {
            Response.Write("<script>window.opener.document.form1.txtFilePath.value='" + fileName + "'</script>");
        }
        else//否则返回父页面DataList中指定的文本框
        {
            Response.Write("<script>window.opener.document.form1.dlList$ctl" + itemid + "$txtFilePath.value='" + fileName + "'</script>");
        }
        Response.Write("<script language=javascript>window.alert('文件上传成功!请不要修改生成的链接路径!');window.close();</script>");
    }

相关文章:

  • 2021-07-04
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2022-02-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2022-02-13
  • 2021-11-09
  • 2021-11-20
相关资源
相似解决方案