problem with uploading/downloading .docx file
-
Hi, I have a problem similar to this post: http://www.microsoft.com.nsatc.net/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.office.developer.vba&tid=6fc07fd9-3313-4c09-9457-f6971ecafda4&cat=en_US_9477b478-d551-48fe-b931-e0d7ed05ac44&lang=en&cr=US&sloc=&p=1 In this post the original poster has written his code in VB whereas I have written my code in C# My code for uploading a file:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.ContentLength>0)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string storagepath = "D:\\Test\\" + filename;
int contentLen = FileUpload1.PostedFile.ContentLength;
Byte[] data = new Byte[contentLen];
FileUpload1.PostedFile.InputStream.Read(data, 0, contentLen);
savefiletodatabase(data);
FileUpload1.SaveAs(storagepath);
}
}private void savefiletodatabase(byte[] data)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
try
{
SqlCommand command = new SqlCommand("INSERT into dbo.test ([file], [filename], [path]) VALUES (@data, '" + FileName + "', '" + FilePath + "')", connection);
command.Parameters.AddWithValue("data", data);
connection.Open();
command.Connection = connection;
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally { connection.Close(); }
}The code to retrieve file goes here:
protected void LinkButton1_Click(object sender, EventArgs e)
{
getfile();
}private void getfile() { SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings\["ConnectionString"\].ToString()); try { SqlCommand command = new SqlCommand("Select \[file\] FROM dbo.test where id='" + Id + "'", connection); SqlDataReader reader; connection.Open(); command.Connection = conn
-
Hi, I have a problem similar to this post: http://www.microsoft.com.nsatc.net/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.office.developer.vba&tid=6fc07fd9-3313-4c09-9457-f6971ecafda4&cat=en_US_9477b478-d551-48fe-b931-e0d7ed05ac44&lang=en&cr=US&sloc=&p=1 In this post the original poster has written his code in VB whereas I have written my code in C# My code for uploading a file:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.ContentLength>0)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string storagepath = "D:\\Test\\" + filename;
int contentLen = FileUpload1.PostedFile.ContentLength;
Byte[] data = new Byte[contentLen];
FileUpload1.PostedFile.InputStream.Read(data, 0, contentLen);
savefiletodatabase(data);
FileUpload1.SaveAs(storagepath);
}
}private void savefiletodatabase(byte[] data)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
try
{
SqlCommand command = new SqlCommand("INSERT into dbo.test ([file], [filename], [path]) VALUES (@data, '" + FileName + "', '" + FilePath + "')", connection);
command.Parameters.AddWithValue("data", data);
connection.Open();
command.Connection = connection;
command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally { connection.Close(); }
}The code to retrieve file goes here:
protected void LinkButton1_Click(object sender, EventArgs e)
{
getfile();
}private void getfile() { SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings\["ConnectionString"\].ToString()); try { SqlCommand command = new SqlCommand("Select \[file\] FROM dbo.test where id='" + Id + "'", connection); SqlDataReader reader; connection.Open(); command.Connection = conn
shankbond wrote:
also when I uploaded a file to the database it was of 18041 bytes, but when I retrieved it after saving on desktop the size was 19,756 bytes which is increased?
I think you need to clear the response using
Response.Clear()
It's very important not to use string concatenation to make sql statements. Use parameterized queries instead.Hesham A. Amin My blog twitter: @HeshamAmin
-
shankbond wrote:
also when I uploaded a file to the database it was of 18041 bytes, but when I retrieved it after saving on desktop the size was 19,756 bytes which is increased?
I think you need to clear the response using
Response.Clear()
It's very important not to use string concatenation to make sql statements. Use parameterized queries instead.Hesham A. Amin My blog twitter: @HeshamAmin