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

Upload sometimes fails and/or stores a corrupted file

1 Answer 406 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Spencer
Top achievements
Rank 1
Spencer asked on 18 Nov 2015, 03:34 PM

I just want to post some of my code and have some eyes on it that may spot something I'm doing wrong. In general, there are just times where a user isn't able to upload a document. A second try usually works though. Literally no idea how to explain this.

 

The other problem I have is seemingly harder to deal with... Sometimes a document gets uploaded (seems to always be a .docx) and when trying to download the document Word attempts to open but says the file is corrupt. I am storing the documents as a VARBINARY(MAX) in sql server. The content type is getting stored as "application/vnd.openxmlformats-officedocument.wordprocessingml.document" which I believe is correct. The one odd thing I noticed one time is that... a particular document was uploaded.  I set a breakpoint when trying to download and saw the "content length" was a specific value. Well, I uploaded this same document to the system and when hitting that same breakpoint the content length value had changed. So one question I have is - does uploading the same document from a different machine (and perhaps a different user) really change the length of the byte array?! I wouldn't think so. I'm not sure if this is even relevant information to my problem but it was just something I noticed and couldn't account for why it happened. And for what its worth, whenever I upload a particular document myself that was a corrupted file after someone else had uploaded, the file is always fine for me. So it's not like the file itself is actually corrupted.

 

Any reason why the radasyncupload might be making these files corrupted? Or is there anything in my code that could explain it?

 

code for uploading:

foreach (UploadedFile File in ClientAgreementUpload.UploadedFiles)
            {
                try
                {
                    byte[] FileData = new Byte[File.ContentLength];
                    File.InputStream.Read(FileData, 0, (int)File.ContentLength);
                    Dictionary<string, object> DocParameters = new Dictionary<string, object>();
                    DocParameters.Add("PreviousDocumentId", DBNull.Value);
                    DocParameters.Add("RequestGroupId", RequestGroupId);
                    DocParameters.Add("Title", File.FileName.ToString());
                    DocParameters.Add("ContentType", File.ContentType.ToString());
                    DocParameters.Add("AgreementDocument", FileData);
                    DocParameters.Add("Comments", DocumentCommentsTextbox.Text);
                    DocParameters.Add("FinalExecuted", 0);
                    DocParameters.Add("SubmitBy", CommonClass.UserName(Request));
                    Session["id"] = CommonClass.Scalar("InsertDocument", ConfigurationManager.ConnectionStrings["ContractAdminConnection"].ToString(), DocParameters);
                }
                catch (Exception error)
                {
                    ...error handling stuff
                }
                
            }


CommonClass.Scalar looks like this:

public static object Scalar(string mySQL, string strConn, Dictionary<string, object> Parameters)
        {
            try
            {
                SqlConnection mySqlConnection = new SqlConnection(strConn);
                SqlCommand mySqlCommand = new SqlCommand(mySQL, mySqlConnection);
                mySqlCommand.CommandType = CommandType.StoredProcedure;
                foreach (KeyValuePair<string, object> Parameter in Parameters)
                {
                    var parameter = Parameter;
                    mySqlCommand.Parameters.Add(new SqlParameter(parameter.Key, parameter.Value));
                }
                object myObject = new object();
                mySqlConnection.Open();
                myObject = mySqlCommand.ExecuteScalar();
                mySqlConnection.Close();
                return myObject;
            }
            catch (System.Exception ex)
            {
                ...error handling stuff
            }
        }

code for downloading... 
protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, object> Parameters = new Dictionary<string, object>();
            Parameters.Add("AgreementDocumentId", Request.QueryString["DownloadParameter"]);
            DataTable AgreementDocumentTable = CommonClass.Table("GetAgreementDocumentBinaryByRequestId", ConfigurationManager.ConnectionStrings["ContractAdminConnection"].ToString(), Parameters);

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = AgreementDocumentTable.Rows[0]["ContentType"].ToString();
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + AgreementDocumentTable.Rows[0]["Title"].ToString());
            Response.AddHeader("Content-Length", ((byte[])AgreementDocumentTable.Rows[0]["AgreementDocument"]).Length.ToString());
            Response.Flush();
            Response.BinaryWrite((byte[])AgreementDocumentTable.Rows[0]["AgreementDocument"]);
            Response.End();
        }

CommonClass.Table is nothing special, just returns a datatable



Here is C# code for the initial call to download a doc: 
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Download", String.Format("GotoDownloadPage(\"./Download.aspx?DownloadParameter={0}\");", Convert.ToInt32(e.CommandArgument)), true);

here is the javascript code for downloading docs:
function GotoDownloadPage(urlString) {
            console.log(urlString);
            window.location = urlString;
        }

1 Answer, 1 is accepted

Sort by
0
Peter Filipov
Telerik team
answered on 23 Nov 2015, 09:13 AM
Hi Spencer,

This is really strange. We have never had similar behavior. In order to find out what might be causing this issue I would like to ask you to test the RadAsyncUpload control on a stand alone page and review the uploaded files manually. This will confirm that the control is not changing the files' content and will help you to narrow down where the problem is coming from.
What happens when the upload is not working, is there an error?

Regards,
Peter Filipov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
AsyncUpload
Asked by
Spencer
Top achievements
Rank 1
Answers by
Peter Filipov
Telerik team
Share this question
or