File upload into database

Posted by Surisetti.rajesh under C# on 4/11/2010 | Views : 4593 | Status : [Member] | Replies : 3
which control is used to upload a file(.Xls,Doc) into database...what is the code..




Responses

Posted by: Abhi2434 on: 4/11/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
To upload a file you need normal FileUpload control which you might use when upload file normally.

Basically to store the file into database, just convert the file into Byte array. Use Encoding.UTF8.GetString to store the bytestream into the database.

You can use Image datatype for the store.

Also remember to store the filename and Content type in the database with the file Bytes. It is essential when you want to retrieve the file.



www.abhisheksur.com

Surisetti.rajesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naveenmanam on: 4/12/2010 [Member] Starter

Up
0
Down
private void btnSubmit_Click(object sender, System.EventArgs e)
{

string strDocExt;
//strDocType to store Document type which will be Stored in the Database
string strDocType;

//Will be used to determine Document length
int intDocLen;

//Stream object used for reading the contents of the Uploading Documnet
Stream objStream;

SqlConnection BooksConn;
SqlCommand cmdUploadDoc;

if(IsValid)
{
if(txtFileContents.PostedFile != null)
{
//Determine File Type
strDocExt = CString.Right
(txtFileContents.PostedFile.FileName,4).ToLower();
switch(strDocExt)
{
case ".doc":
strDocType = "doc";
break;

case ".ppt":
strDocType = "ppt";
break;

case ".htm":
strDocType = "htm";
break;

case ".html":
strDocType = "htm";
break;

case ".jpg":
strDocType = "jpg";
break;

case ".gif":
strDocType = "gif";
break;

default:
strDocType = "txt";
break;
}
//Grab the Content of the Uploaded Document

intDocLen = txtFileContents.PostedFile.ContentLength;
//buffer to hold Document Contents
byte[] Docbuffer = new byte[intDocLen];

//InputStream:
//Gets a Stream object which points to an uploaded Document;
//to prepare for reading the contents of the file.
objStream = txtFileContents.PostedFile.InputStream;

//Store the Content of the Documnet in a buffer
//This buffer will be stored in the Database
objStream.Read(Docbuffer ,0,intDocLen);

//Add Uploaded Documnet to Database as Binary
//You have to change the connection string
BooksConn = new
SqlConnection("Server=Server;UID=sa;Database=Books");

//Setting the SqlCommand
cmdUploadDoc = new
SqlCommand("uSP_BooksUploadFile",BooksConn);
cmdUploadDoc.CommandType = CommandType.StoredProcedure;
cmdUploadDoc.Parameters.Add("@Title ",SqlDbType.VarChar,200);
cmdUploadDoc.Parameters.Add("@Doc",SqlDbType.Image);
cmdUploadDoc.Parameters.Add("@DocType",SqlDbType.VarChar,4);
cmdUploadDoc.Parameters[0].Value = txtTitle.Text;
cmdUploadDoc.Parameters[1].Value = Docbuffer ;
cmdUploadDoc.Parameters[2].Value = strDocType;

BooksConn.Open();
cmdUploadDoc.ExecuteNonQuery();
BooksConn.Close();
}//End of if(txtFileContents.PostedFile != null)
}//End Of if(IsValid)
}//End of Method btnSubmit_Click


NaveenKumar

Surisetti.rajesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Surisetti.rajesh on: 4/13/2010 [Member] Starter

Up
0
Down
Thak u

Surisetti.rajesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response