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 = "";




}

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

一个办法,代码跟踪一下

自己设置断点跟踪调试一下,看下是文件上传失败还是数据库记录失败。

建议如下:
1.文件保存与写入数据库分开调试
2.注意文件保存文件名不能相同
3.可能存在aspx执行文件路径与保存文件路径的匹派问题.以下VB代码供参考:

       Dim oleDbConnection11 As System.Data.OleDb.OleDbConnection
    Dim oleDbDataAdapter11 As System.Data.OleDb.OleDbDataAdapter
    Dim oleDbCommand11 As System.Data.OleDb.OleDbCommand
    Dim reader_one As System.Data.OleDb.OleDbDataReader
    Dim temp As String

    oleDbConnection11 = New OleDbConnection
    oleDbDataAdapter11 = New OleDbDataAdapter
    oleDbCommand11 = New OleDbCommand

    oleDbConnection11.ConnectionString = Session("material_connectionstring")
    oleDbCommand11.Connection = oleDbConnection11


    If Len(FileUpload1.PostedFile.FileName.Trim) = 0 Then
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "PopupScript", "alert('请选择一个上传文件!');", True)
        Return
    End If

    Dim mis, kind, file_path, file_name, upload_name, file_add As String
    Dim upload_id, index_value, file_length, tmp As Double

    mis = Request.QueryString("mis")
    kind = Request.QueryString("kind")
    file_add = Request.QueryString("file_add")
    upload_id = Request.QueryString("upload_id")

重点关注 ** file_path = Left(Request.PhysicalPath, InStrRev(Request.PhysicalPath, "\", -1) - 1) + "\upload\" **

    file_length = FileUpload1.PostedFile.ContentLength
    tmp = InStrRev(FileUpload1.PostedFile.FileName, "\")
    upload_name = Mid(FileUpload1.PostedFile.FileName, tmp + 1, Len(FileUpload1.PostedFile.FileName) - tmp + 1)

    ''返回

    If Len(FileUpload1.PostedFile.FileName) = 0 Then
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "PopupScript", "alert('没有选择上传文件,请检查!');", True)
        Return
    End If

    oleDbConnection11.Open()

    '取最大序号
    temp = "select max(t.index_value) from upload t where t.mis = '"
    temp = temp + mis + "' and t.kind = '"
    temp = temp + kind + "' and t.upload_id = "
    temp = temp + upload_id.ToString

    'TextBox1.Text = temp
    'Return

    oleDbCommand11.CommandText = temp
    reader_one = oleDbCommand11.ExecuteReader()
    Do While reader_one.Read()
        If reader_one.IsDBNull(0) Then
            index_value = 1
        Else
            index_value = (reader_one.GetValue(0) + 1)
        End If
    Loop
    reader_one.Close()
    oleDbConnection11.Close()

    file_name = mis + "_" + kind + "_" + upload_id.ToString + "_" + index_value.ToString + "." + Right(upload_name.Trim, (upload_name.Trim.Length - InStr(upload_name.Trim, ".")))


    '写入数据库

    oleDbConnection11.Open()

    temp = "INSERT INTO upload(mis, kind, upload_id, index_value,file_length,upload_name,file_path, file_name) VALUES('"
    temp = temp + mis + "' , '"
    temp = temp + kind + "' , "
    temp = temp + upload_id.ToString + " , "
    temp = temp + index_value.ToString + " , "
    temp = temp + file_length.ToString + " , '"
    temp = temp + upload_name + "' , '"
    temp = temp + file_path + "' , '"
    temp = temp + file_name + "')"

    oleDbCommand11.CommandText = temp

    oleDbCommand11.ExecuteNonQuery()
    oleDbConnection11.Close()

    '保存文件                       

    FileUpload1.PostedFile.SaveAs(file_path + file_name)

    upload_status.Value = "1"

    'Label1.Text = file_name

错误是什么都不贴出来,谁知道你报什么呀,还有你要注间是不是超大了!

同上,跟踪代码啊,兄弟勤快些,耐心些哦