将大文件上载到Azure数据库会出错(ASP.NET网站)

时间:2015-09-01 20:58:21

标签: c# asp.net vb.net azure

我创建了一个简单的asp.net网站。只有1页,带有文件上传控件和上传按钮。这是它应该如何工作:当用户浏览文件并点击上传时,它会将实际文件保存到我的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数据库吗?

示例上传代码....

    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

3 个答案:

答案 0 :(得分:3)

IIS 7的上限为30 MB,请参阅this link for more info。原因是在上传过程中,ASP.NET将整个文件加载到内存中,然后用户可以将文件保存到磁盘或进一步处理 - 换句话说,没有流式传输。您可以尝试将以下内容添加到Azure Role Startup Task

appcmd set config "My Site/MyApp" -section:requestFiltering -requestLimits.maxAllowedContentLength:104857600 -commitpath:apphost

超出了IIS 30 MB的限制,但我自己没有尝试过。

答案 1 :(得分:0)

What Inge said makes sense. It could also be a timeout associated with the SqlCommand.

Try changing the command timeout: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx

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
    }
}

答案 2 :(得分:0)

对于.Net core 3,Azure SQL数据库和实体框架,添加CommandTimeout对我有帮助。

If dt.Columns.IndexOf("ColumnName") = -1 Then
    'Column not exist
End If