将特定文件的类型上载到数据库中
您好,
我有以下代码设置将文件上传到我的数据库表中。我目前正在努力上传.pst的文件类型。 如果我尝试上传ta@xxx.com.pst并点击上传按钮,则会转到此页面不可用。代码似乎适用于除.pst之外的任何文件类型。 我在窗口8上使用chrome / firefox Web浏览器来测试此代码。
Hello,
I have the following code setup to upload files into my database table. I am currently struggling with uploading file type of ".pst". If I try to upload 'ta@xxx.com.pst' and click the upload button, it goes to 'this is page is not available'. The code seems to work on any file type except .pst. I am using chrome/firefox web browser on window 8, to test this code.
protected void btnUpload_Click(object sender, EventArgs e)
{
//Check whether FileUpload control has file
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string documentType = string.Empty;
//provide document type based on it's extension
switch (fileExtension)
{
case ".pst":
documentType = "application/pst";
break;
case ".pdf":
documentType = "application/pdf";
break;
case ".xls":
documentType = "application/vnd.ms-excel";
break;
case ".xlsx":
documentType = "application/vnd.ms-excel";
break;
case ".jpg":
documentType = "image/jpg";
break;
}
//Calculate size of file to be uploaded
int fileSize = FileUpload1.PostedFile.ContentLength;
//Create array and read the file into it
byte[] documentBinary = new byte[fileSize];
FileUpload1.PostedFile.InputStream.Read(documentBinary, 0, fileSize);
// Create SQL Connection
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
// Create SQL Command and Sql Parameters
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO SaveDoc(DocName,Type,DocData)" +
" VALUES (@DocName,@Type,@DocData)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlParameter DocName = new SqlParameter("@DocName", SqlDbType.VarChar, 50);
DocName.Value = fileName.ToString();
cmd.Parameters.Add(DocName);
SqlParameter Type = new SqlParameter("@Type", SqlDbType.VarChar, 50);
Type.Value = documentType.ToString();
cmd.Parameters.Add(Type);
SqlParameter uploadedDocument = new SqlParameter("@DocData", SqlDbType.Binary,fileSize);
uploadedDocument.Value = documentBinary;
cmd.Parameters.Add(uploadedDocument);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
lblMessage.Text = "File saved to database";
GridView1.DataBind();
}
}
<div>
<asp:FileUpload ID="FileUpload1" runat="server"/>
<asp:Button ID="btnUpload" runat="server"
onclick="btnUpload_Click"
Text="Upload"/>
</div>
如果我遗漏了代码中的内容,请告知我。
非常感谢您的时间和帮助。
If I am missing something in the code, could you please notify me.
Many thanks for your time and help.
查看最大请求长度
asp-net-web-page-not-available [ ^ ]
http ://forums.asp.net/post/4870282.aspx [ ^ ]
fileupload-and- max-length / [ ^ ]
MAX REQUEST LENGTH [ ^ ]
Check out theMax Request Length
asp-net-web-page-not-available[^]
http://forums.asp.net/post/4870282.aspx[^]
fileupload-and-max-length/[^]
MAX REQUEST LENGTH[^]
你的代码(可能)崩溃了
Your code (possibly) crashes at
int result = cmd.ExecuteNonQuery();
如果你附上你的使用'try catch'结构的代码,并在catch块中添加
If you enclose your code with 'try catch' structure, and add
lblMessage.Text=ex.Message;
,你可以看到你的异常。
一些建议:
1-设计多层应用程序(设计一个单独的数据管理类)
2-包含你的UI处理代码在'try catch'结构中,以避免'未处理的异常'和完全崩溃。
3-使用调试器来跟踪代码。
in the catch block, you can see your exception.
Some advices:
1- design multitier application (design a separate class for data management)
2- enclose your UI handling codes in a 'try catch' structure to avoid 'unhandled exception' and a total crash.
3- use the debugger to trace your code.