gpt4 book ai didi

c# - ASP.NET、C#、IIS、MIME 类型、文件上传条件

转载 作者:太空狗 更新时间:2023-10-29 20:34:59 24 4
gpt4 key购买 nike

我在网站上有一个文件上传网络表单,它只需要接受某些格式(或 MIME 类型)...

以下代码工作正常,除了它不会将 .DOCX 文件上传到服务器!这是唯一不起作用的文件类型...我仔细检查了每一行代码,甚至进入 IIS 管理器以确保继承了 .DOCX MIME 类型,并且它们...

有谁知道为什么 .DOCX 文件不会像其他所有文件类型一样上传到服务器?

代码片段:

string savePath = "D:\\HIDDEN PATH HERE";
string fileMsg;

// Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Check to see that the content type is proper and allowed.
// DOC: application/doc, appl/text, application/vnd.msword, application/vnd.ms-word, application/winword, application/word, application/x-msw6, application/x-msword
if (
FileUpload1.PostedFile.ContentType == "text/rtf" ||
FileUpload1.PostedFile.ContentType == "application/doc" ||
FileUpload1.PostedFile.ContentType == "appl/text" ||
FileUpload1.PostedFile.ContentType == "application/vnd.msword" ||
FileUpload1.PostedFile.ContentType == "application/vnd.ms-word" ||
FileUpload1.PostedFile.ContentType == "application/winword" ||
FileUpload1.PostedFile.ContentType == "application/word" ||
FileUpload1.PostedFile.ContentType == "application/msword" ||
FileUpload1.PostedFile.ContentType == "application/x-msw6" ||
FileUpload1.PostedFile.ContentType == "application/x-msword" ||
FileUpload1.PostedFile.ContentType == "application/pdf" ||
FileUpload1.PostedFile.ContentType == "application/x-pdf" ||
FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ||
FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.template"
)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName;

// Append the name of the file to upload to the path.
savePath += strnow + fileName;


// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);

// Notify the user of the name of the file
// was saved under.
//fileMsg = "Your file was saved as " + fileName;
fileMsg = "";
}
else
{
fileMsg = "Your file was not an accepted format. Please use PDF, RTF or DOC formats.";
}

最佳答案

参见 this answer ,它指向您 this page .

DOCX Mime 类型:

application/vnd.openxmlformats-officedocument.wordprocessingml.document

编辑:抱歉,没有在列表中看到它。如果您想允许 DOCX,为什么不检查“.docx”作为扩展名。

|| FileUpload1.PostedFile.FileName.ToLower().Substring(FileUpload1.PostedFile.FileName.Length - 4) == "docx"

关于c# - ASP.NET、C#、IIS、MIME 类型、文件上传条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7390898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com