Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将大文件上载到Azure db时出错(ASP.NET网站)_C#_Asp.net_Vb.net_Azure - Fatal编程技术网

C# 将大文件上载到Azure db时出错(ASP.NET网站)

C# 将大文件上载到Azure db时出错(ASP.NET网站),c#,asp.net,vb.net,azure,C#,Asp.net,Vb.net,Azure,我创建了一个简单的asp.net网站。只有一个页面,带有文件上载控件和上载按钮。这就是它的工作原理:当用户浏览文件并单击“上载”时,它会将实际文件保存到my AZURE数据库中的新记录中 对于小文件,它可以正常工作。对于较大的文件,例如45mb,它会出现以下错误 System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period elapsed prior to completion of t

我创建了一个简单的asp.net网站。只有一个页面,带有文件上载控件和上载按钮。这就是它的工作原理:当用户浏览文件并单击“上载”时,它会将实际文件保存到my AZURE数据库中的新记录中

对于小文件,它可以正常工作。对于较大的文件,例如45mb,它会出现以下错误

System.Data.SqlClient.SqlException (0x80131904): Timeout expired. The timeout period 
elapsed prior to completion of the operation or the server is not responding.
---> System.ComponentModel.Win32Exception (0x80004005):
在我的web.config中,我已经添加了以下代码段

<httpRuntime maxRequestLength="102400000" 
requestValidationMode="2.0" executionTimeout="12000" />
</system.web>

还有

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>

知道会发生什么吗?我错过了什么?由于这是一个Azure db,我还需要其他配置吗

示例上载代码

    Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
            Dim contentType As String = FileUpload1.PostedFile.ContentType
            Using fs As Stream = FileUpload1.PostedFile.InputStream
                Using br As New BinaryReader(fs)
                    Dim bytes As Byte() = br.ReadBytes(fs.Length)
                    Dim constr As String = ConfigurationManager.ConnectionStrings("MyDb").ConnectionString
                    Using con As New SqlConnection(constr)
                        Dim query As String = "insert into MyFileTable values (@ActualDocument, @Filename)"
                        Using cmd As New SqlCommand(query)
                            cmd.Connection = con
                            cmd.Parameters.Add("@ActualDocument", SqlDbType.Binary).Value = bytes
                            cmd.Parameters.Add("@Filename", SqlDbType.VarChar).Value = filename
                            con.Open()
                            Dim i As Integer = cmd.ExecuteNonQuery()
                            Response.Write(i & " records<br/>")
                            con.Close()
                        End Using
                    End Using
                End Using
            End Using
Dim filename As String=Path.GetFileName(FileUpload1.PostedFile.filename)
Dim contentType As String=FileUpload1.PostedFile.contentType
使用fs As Stream=FileUpload1.PostedFile.InputStream
使用br作为新的二进制读取器(fs)
作为字节()的Dim字节=br.ReadBytes(fs.Length)
Dim constr As String=ConfigurationManager.ConnectionString(“MyDb”).ConnectionString
使用con作为新的SqlConnection(cont)
Dim query As String=“插入MyFileTable值(@ActualDocument,@Filename)”
使用cmd作为新的SqlCommand(查询)
cmd.Connection=con
cmd.Parameters.Add(“@ActualDocument”,SqlDbType.Binary).Value=bytes
cmd.Parameters.Add(“@Filename”,SqlDbType.VarChar).Value=Filename
con.Open()
Dim i As Integer=cmd.ExecuteNonQuery()
响应。写入(i&“记录
”) con.Close() 终端使用 终端使用 终端使用 终端使用
IIS 7的上限为30 MB,请参阅。原因是,在上载过程中,ASP.NET会在用户将文件保存到磁盘或进一步处理之前将整个文件加载到内存中—换句话说,没有流。您可以尝试将以下内容添加到:


扩展到IIS 30 MB限制之外,但我自己还没有尝试过。

Inge说的有道理。它也可能是与
SqlCommand
相关联的超时

尝试更改命令超时:


对于.Net core 3、Azure SQL db和实体框架,添加CommandTimeout对我有帮助

services.AddDbContext<YourDbContext>(options => options.UseSqlServer(
    this.Configuration.GetConnectionString("YourConnectionString"),
    sqlServerOptions => sqlServerOptions.CommandTimeout(60))
);
services.AddDbContext(options=>options.UseSqlServer(
this.Configuration.GetConnectionString(“YourConnectionString”),
sqlServerOptions=>sqlServerOptions.CommandTimeout(60))
);

是否有理由或要求将如此大的文件存储在SQL数据库中?我同意@CSharpRocks。如果必须存储大型文件,请尝试使用OneDrive API将文件发送到OneDrive,而不是SQL SERVER。您可能需要一个Office365帐户。您可以发布您正在调用的代码段以上载文件。谢谢您的输入。客户需要Azure并希望将其存储在数据库中。但我会研究并向他提出替代方案供他考虑。任何建议都将不胜感激!我将很快发布一些源代码。设置CommandTimeout似乎有效!我将进一步测试,以确保。优秀和有用的信息!谢谢
using (SqlConnection connection = new SqlConnection(connectionString)) 
{
    connection.Open();
    SqlCommand command = new SqlCommand(queryString, connection);
    // Setting command timeout to 60 seconds
    command.CommandTimeout = 60;
    try {
         command.ExecuteNonQuery();
    }
    catch (SqlException e) {
         // do something with the exception
    }
}
services.AddDbContext<YourDbContext>(options => options.UseSqlServer(
    this.Configuration.GetConnectionString("YourConnectionString"),
    sqlServerOptions => sqlServerOptions.CommandTimeout(60))
);