将文件上传到服务器无效

时间:2013-02-05 10:28:49

标签: asp.net file-upload

我正在努力将图像上传到服务器,代码能够在localhost中工作,但是当我在服务器上运行代码时,代码无法成功运行。

我的服务器路径是server / servername / website(网站是我所有文件都可用的文件夹)

  • 你可以看到/ * - * /下的代码无效,文件能够上传到指定的文件夹但是没有运行sql代码
  • 指定的文件夹位于网站文件夹中:Design
下面的

是代码的cs文件:

protected void btnSubmit_Click(object sender, EventArgs e)


{       
    if (IsPostBack)
    { 
        Boolean fileOK = false;
        String path = Server.MapPath("~/website/Design");
        if (FileUpload1.HasFile)
        {
            String fileExtension =
                System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }
        if (fileOK)
        {

            try
            {
                if (txtFileName.Text == "")
                {
                    lblUpload.Text = "Please input design description of your upload!";
                }



                    FileUpload1.PostedFile.SaveAs(path
                        + FileUpload1.FileName);
                    /*lblUpload.Text = "File uploaded!";

                    string pathName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    String filename = ("~/Design/") + FileUpload1.FileName;
                    OleDbConnection mDB = new OleDbConnection();
                    mDB.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source="
                        + Server.MapPath("~/website/App_Data/ImageDB.accdb");
                    mDB.Open();
                    OleDbCommand cmd;
                    string strUserId = (string)Session["sUserId"];
                    string strSQLInsert = "INSERT INTO "
                    + " Design (dUserId ,dFileName, dFileData, dContentType, dFileSize, dFilePath)"
                    + " VALUES (@dUserId, @filename, @data, @content, @size, @path)";

                    cmd = new OleDbCommand(strSQLInsert, mDB);
                    cmd.Parameters.AddWithValue("@dUserId", strUserId);
                    cmd.Parameters.AddWithValue("@filename", txtFileName.Text);
                    cmd.Parameters.AddWithValue("@data", filename);
                    cmd.Parameters.Add("@content", OleDbType.VarChar, 100).Value = FileUpload1.PostedFile.ContentType;
                    cmd.Parameters.Add("@size", OleDbType.BigInt, 555555).Value = FileUpload1.PostedFile.ContentLength;
                    cmd.Parameters.AddWithValue("@path", "website/Design/" + pathName);

                    cmd.ExecuteNonQuery();
                    mDB.Close();*/

                    Type csType = this.GetType();
                    ClientScript.RegisterStartupScript(csType, "Success", scriptSuccessUpload);
                    lblUpload.Visible = false;

            }

            catch (Exception ex)
            {
                //lblUpload.Text = "File could not be uploaded.";
                lblUpload.Text = "File uploaded!";

           }
        }
        else
        {
            lblUpload.Text = "Cannot accept files of this type.";

        }
    }
}

0 个答案:

没有答案