仅上传文档文件 [英] upload only document files
本文介绍了仅上传文档文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试上传文件.user可以上传theri文件但是他/她可以上传图片而不是文件而且我想限制这个如何申请条件这是我的上传代码
i am trying to upload documents .user can be able to upload theri documents but he/she can be upload images istead of documents and i want to d restrict about this how to apply condition this is my upload code
if (FileUploadControl.PostedFile != null &&
FileUploadControl.PostedFile.ContentLength
> 0)
{
if
(FileUploadControl.FileContent.Length < 100000)
{
string filename =
Path.GetFileName(FileUploadControl.PostedFile.FileName);
string folder = Server.MapPath("~/Docfiles/");
Directory.CreateDirectory(folder);
FileUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
try
{
cc.upload1(Txt_docde.Value, txt_dname.Value,
FileUploadControl.FileName, Convert.ToInt32(Docdrop.SelectedValue),
Convert.ToInt32(DropDownList2.SelectedValue),
Convert.ToString(Session["Login2"]),Convert.ToInt32(Session["UserID"]));
StatusLabel.ForeColor = System.Drawing.Color.Green;
//StatusLabel.ForeColor = System.Drawing.FontStyle.Bold;
StatusLabel.Text = "Success";
}
catch
{
StatusLabel.ForeColor = System.Drawing.Color.Red;
Label2.Text = "Failed";
}
}
else
{
StatusLabel.ForeColor = System.Drawing.Color.Red;
Label2.Text = "File Size to big";
}
}
推荐答案
您只需添加一个检查文件扩展名的if语句即可。我过去做过类似的事情:
You can just add an if statement that checks the file extension. I have done something like this in the past:
if (filename.EndsWith(".doc"))
{
//do stuff
}
或者,
Or,
if (FileUploadControl.FileName.EndsWith(".doc"))
{
//do stuff
}
是..
在验证操作上添加if else语句。
Yes..
Add if else statement on your validation action.
string typedoc = fileupload1.PostedFile.FileName.Split('.').Last();
if(typedoc == "doc" || typedoc == "docx")
{
//save
}
else
{
//alert error with massage box or label
}
您可以使用上面建议的解决方案,也可以使用下面的正则表达式验证
hi either you for the solution suggested above or you can use the below regular expression for validation
<asp:RegularExpressionValidator ID="uplValidator" runat="server" ControlToValidate="FileUpload1"
ErrorMessage=".doc, .docx formats are allowed"
ValidationExpression="(.+\.([d][o][c])|.+\.([d][o][c][x]))"></asp:RegularExpressionValidator>
这篇关于仅上传文档文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文