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