This is a migrated thread and some comments may be shown as answers.

Errors while uploading File to database

1 Answer 162 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
scrut
Top achievements
Rank 1
scrut asked on 20 May 2008, 03:42 PM

I am trying to upload files to a database and then let the user download them again.

But when I upload a small file, it seems like only few kB actually make it into the database. The progress bar shows up for a split second only and then disappears. The entry is written is to the database but the binary content is too small.

When I upload a large file (e.g. 10 MB), I get a "page cannot be displayed" error.

Any idea? Thanks so much! s.


Here's some code:

aspx:

<

telerik:RadUpload ID="RadUpload" runat="server" Skin="Telerik" InitialFileInputsCount="1" Width="620px" ControlObjectsVisibility="None" InputSize="100" OverwriteExistingFiles="True" />


cs:

protected void SaveButton_Click(object sender, EventArgs e)  
    {  
        if (RadUpload.UploadedFiles.Count > 0)  
        {  
            // Obtain the USER ID from the Session  
            this.userid = HttpContext.Current.Session["USERID"].ToString();  
            if (String.IsNullOrEmpty(this.userid))  
                this.userid = SATConfiguration.LocalUser;  
 
            // Obtain the description and clear it of "junk"  
            this.description = Regex.Replace(this.DescriptionTextBox.Text, "[\t\r\n\v\a\b\f\0\\\'\"]", String.Empty, RegexOptions.Compiled);  
 
            // Get the file and its name  
            file = RadUpload.UploadedFiles[0];  
            filefilename = file.GetName();  
 
            //Read the file  
            bytes = new byte[file.InputStream.Length];  
            file.InputStream.Read(bytes, 0, (int)file.InputStream.Length);  
 
            // Call the stored procedure AddAttachment with its parameters  
            using (SqlConnection conn = new SqlConnection(SATConfiguration.SatConnectionString))  
            {  
                try  
                {  
                    // Open a new connection  
                    conn.Open();  
 
                    // Create a command object identifying the stored procedure  
                    SqlCommand cmd = new SqlCommand("AddAttachment", conn);  
 
                    // Set the command object so it knows to execute a stored procedure  
                    cmd.CommandType = CommandType.StoredProcedure;  
 
                    // Add parameters to command, which will be passed to the stored procedure  
                    cmd.Parameters.AddWithValue("@userid", this.userid);  
                    cmd.Parameters.AddWithValue("@caseid", this.caseid);  
                    cmd.Parameters.AddWithValue("@filename", this.filename);  
                    cmd.Parameters.AddWithValue("@description", this.description);  
                    cmd.Parameters.AddWithValue("@attachment", this.bytes);  
 
                    cmd.ExecuteNonQuery();  
                    cmd.Dispose();  
                }  
                catch (Exception ex)  
                {  
                    log.Error("Exception while writing attachment to database", ex);  
                }  
                finally  
                {  
                    // close the connection  
                    conn.Close();  
                }  
            }  
 

 

1 Answer, 1 is accepted

Sort by
0
Erjan Gavalji
Telerik team
answered on 22 May 2008, 10:50 AM
Hi scrut,

You might be experiencing a combination of two issues. Can you check if you've allowed large file uploads through the <httpRuntime> section in your application's web.config file? As per the "only few kB actually make it into the database" - could it be caused by the field type in the database? Is the type of the field set to image?

Regards,
Erjan Gavalji
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Upload (Obsolete)
Asked by
scrut
Top achievements
Rank 1
Answers by
Erjan Gavalji
Telerik team
Share this question
or