ASP.Net:将文件上传到服务器上的数据库

问题描述:





我正在ASP.net 3.5开发一个网站。我想开发一种上传* .doc格式的简历的方法。如何通过删除和更新操作从服务器和数据库上传和下载MS Word文档?



谢谢。



Debapriya





[orig。标题]

如何使用删除和更新操作在服务器和数据库中上传和下载word文档。

For上传文件,阅读教程@ http://asp.net-tutorials.com/controls/file- upload-control / [ ^ ]



如需下载,请参阅 http: //www.west-wind.com/weblog/posts/76293.aspx [ ^ ]
For uploading file, read the tutorial @ http://asp.net-tutorials.com/controls/file-upload-control/[^]

For downloading see http://www.west-wind.com/weblog/posts/76293.aspx[^]


尝试

{//读取文件并将其转换为字节数组>
string filePath = FileUpload1.PostedFile.FileName;

string filename = Path.GetFileName(filePath);

string ext = Path.GetExtension(filename);

string contenttype = String.Empty;

//根据文件扩展名设置contenttype

开关(分机)

{

case.doc:

contenttype =application / vnd.ms-word;

休息;

case.docx:

contenttype =application / vnd.ms-word;

break ;

}

if(contenttype!= String.Empty)

{Stream fs = FileUpload1.PostedFile.InputStream;

BinaryReader br = new BinaryReader(fs);

Byte [] bytes = br.ReadBytes((Int32)fs.Length);

//插入文件进入数据库

string strQuery = 插入职业(cv,extcv,namecv)值(@ cv,@ \\ textcv,@ namecv);

SqlCommand cmd = new SqlCommand(strQuery);

/ /简历的名称

cmd.Parameters.Add(@ namecv,SqlDbType.NVarChar).Value = filename;

//简历简历文件

cmd.Parameters.Add(@ extcv,SqlDbType.NVarChar).Value = contenttype;

// CV DATA BINARY

cmd.Parameters .Add(@ cv,SqlDbType.Binary).Value = bytes;

//函数bolean

InsertUpdateData(cmd);

//回复另一个页面

Response.Redirect(done.aspx);

}

else

{lblMessage.ForeColor = System.Drawing.Color.Red;

lblMessage。 Text =无法识别文件格式。上传图片/文字/ PDF / Excel格式;

}



}



catch(Exception exr)

{Response.Write(exr.Message);

}





}

//用于插入文件数据库的功能

private布尔值InsertUpdateData(SqlCommand cmd)

{

String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings [dballharam]。ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

// BUTTOM CLICK中的命令文本

cmd.CommandType = CommandType.Text;

cmd.Connection = con;

try

{

con.Open();

cmd.ExecuteNonQuery();

返回true;

}

catch(例外情况)

{

Response.Write(ex.Message);

返回false;

}

终于

{

con.Close();

con.Dispose();

}

}
try
{ // Read the file and convert it to Byte Array
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
//Set the contenttype based on File Extension
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
}
if (contenttype != String.Empty)
{ Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
//insert the file into database
string strQuery = "insert into careers(cv,extcv,namecv) values (@cv,@extcv,@namecv)";
SqlCommand cmd = new SqlCommand(strQuery);
// NAME OF THE CV
cmd.Parameters.Add("@namecv", SqlDbType.NVarChar).Value = filename;
//EXTENTION OF THE CV DOCUMENT
cmd.Parameters.Add("@extcv", SqlDbType.NVarChar).Value = contenttype;
//CV DATA BINARY
cmd.Parameters.Add("@cv", SqlDbType.Binary).Value = bytes;
//function bolean
InsertUpdateData(cmd);
//RESPONSE TO ANOTHER PAGE
Response.Redirect("done.aspx");
}
else
{ lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "File format not recognised. Upload Image/Word/PDF/Excel formats";
}

}

catch (Exception exr)
{Response.Write(exr.Message);
}


}
// THE FUUNCTION IN FOR INSERT THE FILE DB
private Boolean InsertUpdateData(SqlCommand cmd)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["dballharam"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
//COMMAND TEXT IN BUTTOM CLICK
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}