How to save a word document in the database.
protected void btnFileUpload_Click()
{
byte[] fileData = null;
try
{
string FileExtention;
if ((m_File != null)&&(m_File.PostedFile != null) && (m_File.PostedFile.ContentLength > 0))
{
int size = m_File.PostedFile.ContentLength;
if (size < 102400)
{
if (ExtentionOK("pdf") || ExtentionOK("doc") || ExtentionOK("rtf") || ExtentionOK("txt"))
{
FileExtention = System.IO.Path.GetExtension(m_File.PostedFile.FileName);
string fn = System.IO.Path.GetFileName(m_File.PostedFile.FileName);
Stream MyStream = m_File.PostedFile.InputStream;
long iLength = MyStream.Length;
fileData = new byte[(int)MyStream.Length];
MyStream.Read(fileData, 0, (int)MyStream.Length);
MyStream.Close();
SqlConnection MyConnection = new SqlConnection("Data Source=myserver;Integrated Security=SSPI;Initial Catalog=Mydatabase");
MyConnection.Open();
SqlCommand MyCommand = new SqlCommand("INSERT INTO mytable (myfile) VALUES (@doc_content)", MyConnection);
SqlParameter param1 = new SqlParameter("@doc_content", SqlDbType.Image);
param1.Value = fileData;
MyCommand.Parameters.Add(param1);
MyCommand.ExecuteNonQuery();
}
else
Reponse.Write("File extention not allowed");
}
else
Reponse.Write("File too big. You are only allowed 100K for the resume");
}
else
Reponse.Write("Please select a file to upload");
}
catch (Exception excep)
{
ExceptionCatching(excep);
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment