【问题标题】:Inserting files (pdf etc..) into a database using asp.net c# [duplicate]使用asp.net c#将文件(pdf等)插入数据库[重复]
【发布时间】:2018-08-24 04:49:01
【问题描述】:

我想创建一个文档矩阵,允许用户将文件上传到基于 Web 的数据库中。 重新升级到访问文件路径时出现错误。 System.IO.FileNotFoundException: '找不到文件'C:\Program Files (x86)\IIS Express\auto.docx'。'

数据库中的数据类型是 Varbinary(MAX)。 与数据库的连接也没有问题。

这里是上传文件的按钮事件。

    protected void Button1_Click(object sender, EventArgs e)
    {
        //get file path from file upload button 
         String filepath=    System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);

        //Converts file(.txt pdf etc..) into byte[]
        byte[] filedata = System.IO.File.ReadAllBytes(filepath);

       //Insert file into database
        cmd.CommandText = "INSERT INTO tbl_test (Name)" +
                                    " Values ('" + TextBox1.Text + "','" + filedata + "')";
        cmd.Connection = mycon;
        cmd.ExecuteNonQuery();
        mycon.Close();
    }

【问题讨论】:

  • 只是添加。这是我机器上的本地文件。我在想访问文件可能存在安全问题。
  • System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName); 没有意义。该文件不在文件系统上。它在记忆中。
  • 如果路径错误,正确的方法是什么
  • 但我使用的是文件上传按钮。它允许我浏览和选择一个文件。通过我选择文件,可以找到路径。因此可以生成路径。

标签: c# sql asp.net database visual-studio


【解决方案1】:

您是否有机会使用 Windows 10?

如果是,该文件夹是否为“只读”文件夹?我去年遇到过这个问题,可以检查一下以确认我是如何解决的。

if (FileUpload1.PostedFile != null)
    {
        filePath = FileUpload1.PostedFile.FileName; // gets the file name with path.
    }

【讨论】:

  • 是的,我在 Windows 10 上。谢谢
  • 这无关紧要。该文件不在文件系统上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多