【问题标题】:How to process uploaded files in ASP.NET core 2.0如何在 ASP.NET core 2.0 中处理上传的文件
【发布时间】:2018-03-08 16:06:13
【问题描述】:

我们正在尝试将我们的代码从前段时间编写的 ASP.NET 迁移到 ASP.NET Core 2.0。

这段代码在 SQL Server 中存储一个文档并检索它。

***Original Code:*** 

protected void btnUpload_Click(object sender, EventArgs e)
{
    foreach (HttpPostedFile postedFile in multipleUpload.PostedFiles)
    {
        string filename = Path.GetFileName(postedFile.FileName);
        string contentType = postedFile.ContentType;
        using (Stream fs = postedFile.InputStream)
        {
            using (BinaryReader br = new BinaryReader(fs))
            {
                byte[] bytes = br.ReadBytes((Int32)fs.Length);
                string constr = ConfigurationManager.ConnectionStrings["ab"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string query = "insert into ftr_UploadMultiple (name,contentType,data) values (@Name, @ContentType, @Data)";
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        cmd.Connection = con;
                        cmd.Parameters.AddWithValue("@Name", filename);
                        cmd.Parameters.AddWithValue("@ContentType", contentType);
                        cmd.Parameters.AddWithValue("@Data", bytes);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();

我们确实尝试了以下代码,它只在数据库中存储了 0 个字节: 围绕此的任何建议都应该非常有帮助。

我们在 ASP.NET Core 2.0 中的代码

if (file.Length > 0)
{
    using (var stream = new
    FileStream(filePath, FileMode.Create))
    {
        await file.CopyToAsync(stream);
        using (BinaryReader br = new BinaryReader(stream))
        {
            byte[] bytes = br.ReadBytes((Int32)stream.Length);
            string constr = "<Connection String";
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "insert into ftr_UploadMultiple (data) values (@Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();

我特意删除了结尾}s。此外,在 ASP.NET Core 2.0 中无法将已上传的文件下载为 Response.Binarywrite() 时遇到问题。

【问题讨论】:

    标签: c# asp.net asp.net-core asp.net-core-2.0


    【解决方案1】:

    调用CopyToAsync将上传文件中的字节复制到文件流后,文件流的位置在末尾。然后,当您尝试从文件流中读取数据时,您只会读取最后的空字节,从而导致读取 0 个字节。

    最简单的解决方案是在阅读之前添加以下内容:

    stream.Position = 0;
    

    但是,除非您确实需要将文件写入文件系统,否则这只是无关紧要的工作。最好将上传文件的流复制到MemoryStream,然后简单地使用ToArray 从中获取字节:不需要额外的阅读器。

    【讨论】:

    • 谢谢@Chris Pratt。第一部分奏效了。 MemoryStream ms = new MemoryStream();stream.Position = 0;ms.Position = 0;流。复制到(毫秒);在查询字符串中: cmd.Parameters.AddWithValue("@Data", ms.ToArray());是我写的。这似乎也占用了最后一个字节。我尝试将 stream.Position 和 ms.Position 设置为 0 并删除它。
    【解决方案2】:

    尝试从 InputStream 中获取字节数组

    // Read bytes from http input stream
    BinaryReader b = new BinaryReader(file.InputStream);
    byte[] binData = b.ReadBytes(file.ContentLength);
    

    ASP.NET Core

    if (file.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(file.InputStream))
        {
            /* ... use file.Length or file.ContentLength */
    
            byte[] bytes = br.ReadBytes(file.Length);
    
            /* ... File Processing (bytes) */           
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2018-02-22
      • 2019-02-23
      • 2014-03-14
      相关资源
      最近更新 更多