Arrhythmias 2015-05-22 08:19 采纳率: 25%
浏览 2856

ASP.NET文件上传问题,一上传就提示失败

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class xueshengzuoye: System.Web.UI.Page
{
public static string fname;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

        if (Request["ID"] != null)
        {
            InitData();
        }
    }
}
private void FileUpLoad()
{
    if (UploadFile.Value != null)
    {
        string nam = UploadFile.PostedFile.FileName;
        //取得文件名(抱括路径)里最后一个"."的索引
        int i = nam.LastIndexOf(".");
        //取得文件扩展名
        string newext = nam.Substring(i);
        //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
        DateTime now = DateTime.Now;
        string newname = now.ToFileTimeUtc() + UploadFile.PostedFile.ContentLength.ToString();
        //保存文件到你所要的目录,这里是IIS根目录下的uploadfiles目录
        //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
        UploadFile.PostedFile.SaveAs(Server.MapPath("UpLoad/" + newname + newext));
        //得到这个文件的相关属性:文件名,文件类型,文件大小
        fname = "UpLoad/" + newname + newext;

    }
}
protected void InitData()
{

    string id = Request["ID"].ToString();

    try
    {

        string SQL = "select * from TB_xueshengzuoye where ID='" + id + "'";
        DataTable dt = DBHelper.GetDataSet(SQL).Tables[0];

        if (dt.Rows.Count > 0)
        {

            this.TextBox1.Text = dt.Rows[0]["ZuoYeName"].ToString();



        }
    }
    catch
    {
    }

}



protected void Button1_Click(object sender, EventArgs e)
{
    if (UploadFile.Value != null && UploadFile.Value != "")
    {
        FileUpLoad();
    }
    string SQL = "";
    bool iRet = false;
    if (Request["ID"] != null)
    {
        string id = Request["ID"].ToString();
        SQL = "update TB_xueshengzuoye set ZuoYeName='" + this.TextBox1.Text + "',URl='" + fname + "' where ID='" + id + "'";
        iRet = DBHelper.ExecSql(SQL);

    }
    else
    {
        SQL = "insert into TB_xueshengzuoye(ZuoYeName,URl,AddUser ) values('" + this.TextBox1.Text + "','" + fname + "','" + Session["username"].ToString() + "')";
        iRet = DBHelper.ExecSql(SQL);
    }


    if (iRet)
    {

        Response.Write("<script>alert('恭喜您,操作成功!');document.location=document.location;</script>");
    }
    else
    {
        Response.Write("<script>alert('对不起,操作失败!');document.location=document.location;</script>");
    }
}

protected void Button2_Click(object sender, EventArgs e)
{


    this.TextBox1.Text = "";




}

}
为什么一上传文件就提示对不起,操作失败!

  • 写回答

5条回答 默认 最新

  • 老李家的小二 2015-05-22 08:48
    关注

    一个办法,代码跟踪一下

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符