C# 上载工作不正常,给出有关文件类型的错误消息
我有一个web控件和后端代码,用于将文件上载到服务器(在测试环境中,是解决方案的文件夹) 我正在使用的错误消息包括“not DOCX”和“File size too big”,当前控件没有上载文件,只输出“is not DOCX”C# 上载工作不正常,给出有关文件类型的错误消息,c#,asp.net,.net,upload,C#,Asp.net,.net,Upload,我有一个web控件和后端代码,用于将文件上载到服务器(在测试环境中,是解决方案的文件夹) 我正在使用的错误消息包括“not DOCX”和“File size too big”,当前控件没有上载文件,只输出“is not DOCX” 使用系统; 使用System.Collections.Generic; 使用System.IO; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControl; 命名空间文件上载 {
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间文件上载
{
//这个web控件的后端将用于上载一个文件,该文件将在页面上提取并显示其XML标记。
//此代码检查fileupload控件中是否有输入,然后进入保存文档的下一页。
公共部分类UploadFileControl:System.Web.UI.UserControl
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
//命名当用户尝试上载表单时将使用的脚本管理器/如果他们错误地尝试上载,则给出错误
受保护的无效上载按钮\u单击(对象发送者,事件参数e)
{
//如果文件位于
if(FileUploadControl.HasFile)
{
尝试
{
//允许文档/docx的内容类型
if(FileUploadControl.PostedFile.ContentType==“document/docx”)
{
//如果文件大小小于51mb
if(FileUploadControl.PostedFile.ContentLength<2000)
{
//命名文件名,找到名称的路径
字符串filename=Path.GetFileName(FileUploadControl.filename);
//服务器上传路径(我们只需要将其保存为变量,以便在下一页中找到,因为它将被创建/删除
FileUploadControl.SaveAs(Server.MapPath(“~/”)+文件名);
//使用上载的文件更新标签
StatusLabel.Text=“上载状态:文件已上载!”;
//移动到模板向导页面
重定向(“sitename.com”,false);
//将用于获取文档字符串
返回;
}
其他的
//显示文件需要小于的大小
StatusLabel.Text=“上传状态:文件必须小于2mb!”;
}
其他的
//告诉用户仅接受docx文件
StatusLabel.Text=“上载状态:仅接受DOCX文件!”;
}
捕获(例外情况除外)
{
//显示异常消息,在这种情况下,它可能是size/type/如果存在
StatusLabel.Text=“上载状态:无法上载文件。出现以下错误:“+ex.Message;
}
}
}
}
}
web控件的前面
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadFileControl.ascx.cs" Inherits="FileUpload.UploadFileControl" %>
<asp:FileUpload id="FileUploadControl" runat="server" />
<asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
<br /><br />
<asp:Label runat="server" id="StatusLabel" text="Upload status: " />
还有我的网页表单
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="FileUpload.test" %>
<%@ Register Src="~/UploadFileControl.ascx" TagPrefix="uc1" TagName="UploadFileControl" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="uploadForm" runat="server">
<div>
<uc1:UploadFileControl runat="server" ID="UploadFileControl" />
</div>
</form>
</body>
</html>
使用文件扩展名检查文件类型,如下所示
if(Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower() ==".docx")
{
// file type is docx.. do something
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
使用文件扩展名检查文件类型,如下所示
if(Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower() ==".docx")
{
// file type is docx.. do something
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
使用文件扩展名检查文件类型,如下所示
if(Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower() ==".docx")
{
// file type is docx.. do something
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
使用文件扩展名检查文件类型,如下所示
if(Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower() ==".docx")
{
// file type is docx.. do something
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
DOCX内容类型似乎如下所示:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
您可以看到MS Office内容类型的列表
因此,您的代码应该是:
if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
//upload
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
DOCX内容类型似乎如下所示:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
您可以看到MS Office内容类型的列表
因此,您的代码应该是:
if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
//upload
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
DOCX内容类型似乎如下所示:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
您可以看到MS Office内容类型的列表
因此,您的代码应该是:
if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
//upload
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
DOCX内容类型似乎如下所示:
application/vnd.openxmlformats-officedocument.wordprocessingml.document
您可以看到MS Office内容类型的列表
因此,您的代码应该是:
if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
//upload
}
else
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
我想问题在于这条线
if (FileUploadControl.PostedFile.ContentType == "document/docx")
您可以参考以下链接了解不同的内容类型吗
我想问题出在这条线路上
if (FileUploadControl.PostedFile.ContentType == "document/docx")
您可以参考以下链接了解不同的内容类型吗
我想问题出在这条线路上
if (FileUploadControl.PostedFile.ContentType == "document/docx")
您可以参考以下链接了解不同的内容类型吗
我想问题出在这条线路上
if (FileUploadControl.PostedFile.ContentType == "document/docx")
您可以参考以下链接了解不同的内容类型吗
我不会检查文件扩展名,因为对用户来说,重命名文件扩展名并破解应用程序非常简单;我不会检查文件扩展名,因为它非常简单;对用户来说,重命名文件扩展名并破解应用程序,我不会检查文件扩展名,因为对用户来说,重命名文件扩展名并破解应用程序非常简单d不检查文件扩展名,因为对用户来说,重命名文件扩展名并破解应用程序非常简单