WebDevGuy2
WebDevGuy2

Reputation: 1249

Uploading large file to Azure db gives error (ASP.NET website)

I created a simple asp.net website. There's only 1 page, with a file upload control and an upload button. This is how it's supposed to work: When the user browses to the file and clicks upload it will save the actual file to a new record in my AZURE database.

With small files it works fine. With larger files, such as 45mb, it gives the following error....

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):

In my web.config I already added the following snippets....

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

And….

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

Any idea what could be going on? What am I missing? Are there any additional configurations I need since this is an Azure db?

Sample upload code....

    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

Upvotes: 1

Views: 506

Answers (3)

Morten Engh
Morten Engh

Reputation: 1

For .Net core 3, Azure SQL db and entity framework, adding CommandTimeout helped me.

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

Upvotes: 0

Shaun Luttin
Shaun Luttin

Reputation: 141512

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

Upvotes: 0

user152949
user152949

Reputation:

IIS 7 has a cap of 30 MB, see this link for more info. The reason for this is that during the upload process, ASP.NET loads the whole file in memory before the user can save the file to the disk or processed further - no streaming in other words. You could try to add the following to a Azure Role Startup Task:

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

to extend beyond the IIS 30 MB limit, but I have not tried it myself.

Upvotes: 3

Related Questions