【问题标题】:upload file on HTTP server using POST gives 0 for HttpFileCollection?使用 POST 在 HTTP 服务器上上传文件为 HttpFileCollection 提供 0?
【发布时间】:2012-04-25 05:10:05
【问题描述】:

我正在尝试使用 POST 在 HTTP 服务器上上传文件,但是当我调用它时会给出

uploadFile.Count = 0 amd 不进入 if 语句。文件路径正确。

客户端代码

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fileToUpload = Server.MapPath("~/Files/Ricky_Martin_Livin_la.mp3");
            string uploadUrl = "http://localhost/soundcheck/uploadfiles.aspx";
            //string uploadUrl = "http://10.0.2.2/musicapp/handle_upload.php";
            FileStream rdr = new FileStream(fileToUpload, FileMode.Open);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
            req.Method = "POST"; // you might use "POST"
            req.ContentLength = rdr.Length;
            req.AllowWriteStreamBuffering = true;

            Stream reqStream = req.GetRequestStream();

            byte[] inData = new byte[rdr.Length];

            // Get data from upload file to inData 
            int bytesRead = rdr.Read(inData, 0, int.Parse(rdr.Length.ToString()));

            // put data into request stream
            reqStream.Write(inData, 0, int.Parse(rdr.Length.ToString()));

            rdr.Close();
            req.GetResponse();

            // after uploading close stream 
            reqStream.Close();
        }
    }

服务器代码

using System;
using System.Collections;
using System.IO;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;

public partial class uploadfiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            LogInFile("First");
            HttpFileCollection uploadFile = Request.Files;
            LogInFile("Second");
            LogInFile(uploadFile.Count.ToString());
            if (uploadFile.Count > 0)
            {
                HttpPostedFile postedFile = uploadFile[0];
                LogInFile("Thrid");
                System.IO.Stream inStream = postedFile.InputStream;
                LogInFile("Forth");
                byte[] fileData = new byte[postedFile.ContentLength];
                LogInFile("Fifth");
                inStream.Read(fileData, 0, postedFile.ContentLength);
                LogInFile("Sixth");
                postedFile.SaveAs(Server.MapPath("Data") + "\\" + postedFile.FileName);
            }        
        }
        catch (Exception ex)
        { 
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Message : " +ex.Message);
            sb.AppendLine("Source : " + ex.Source);
            sb.AppendLine("StackTrace : " + ex.StackTrace);
            sb.AppendLine("InnerException : " + ex.InnerException);
            sb.AppendLine("ToString : " + ex.ToString());

            LogInFile(sb.ToString());
        }
    }
    public void LogInFile(string str)
    {
        StringBuilder sb = new StringBuilder();
        using (StreamReader sr = new StreamReader(Server.MapPath("Data") + "\\expfile.txt"))
        {            
            sb.AppendLine("= = = = = =");
            sb.Append(sr.ReadToEnd());
            sb.AppendLine();
            sb.AppendLine();
        }
        sb.AppendLine(str);
        using (StreamWriter outfile = new StreamWriter(Server.MapPath("Data") + "\\expfile.txt"))
        {
            outfile.Write(sb.ToString());
        }
    }

}

在服务器代码中,我编写这些日志来跟踪是否有任何错误或错误发生在哪一行。

在服务器代码中,它给出 uploadFile.Count = 0 并且不进入 在调试模式下,客户端代码成功执行,但在页面上显示此消息

您可以从服务器/客户端代码中看到.. 不涉及数据库。

【问题讨论】:

  • 请张贴您的 html 表单。 sa 的登录失败也是 asp.net 无法登录到您的数据库
  • 您可以从代码中看到没有涉及数据库..甚至没有数据库访问代码
  • 那么您发布的错误与数据库相关。也许代码在项目的其他地方。也许是 global.asax?

标签: c# asp.net .net http-post


【解决方案1】:

我认为您提供的数据库凭据不正确。

【讨论】:

  • 您可以从代码中看到没有涉及数据库..甚至没有数据库访问代码
  • 他指的是消息而不是代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多