图片上传为什么文件格式无法用ContentType获取?

研究汽车的米舞指 2006-11-01 10:31:00
我的代码是这样的:
a.aspx:

<form id="Form1" name="Form1" method="post" runat="server" >
<INPUT type="file" size="50" name="file1" id="file1" style="WIDTH:300px" runat="server">
</form>

b.aspx.cx:
if (TestPostFiles() == true)
{}

private bool TestPostFiles()
{
HttpPostedFile ImgFile = Request.Files["file1"];
string [] PicType=new string[]{".gif",".jpg",".jpeg",".png"};
string UpFileType=ImgFile.ContentType.ToLower();


int cnt=0;
for(int x=0;x<PicType.Length;x++)
{
if(UpFileType==PicType[x])
{
cnt++;
break;
}
}
if(cnt<1)
{
Response.Write("<script>alert('图片格式不支持');history.go(-1)</script>");
return false;

}
else
{
return true;
}
}

虽然文件能正常保存,其他都正常,就是格式得不到.无论我上传任何格式的东西,包括jpg..得到的UpFileType始终是:总是的到:是"application/octet-stream"
这是为什么?请高手帮忙!!!
...全文
371 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackant2 2006-11-01
  • 打赏
  • 举报
回复
string [] PicType=new string[]{"image/jpeg","image/gif","image/bmp","image/png"};
blackant2 2006-11-01
  • 打赏
  • 举报
回复
image/jpeg
  • 打赏
  • 举报
回复
csdn又出问题了??????
  • 打赏
  • 举报
回复
up!为什么在列表中我看不到自己的帖子!!?
blackant2 2006-11-01
  • 打赏
  • 举报
回复
看看结果是不是
image/jpeg
image/gif
image/bmp
........
blackant2 2006-11-01
  • 打赏
  • 举报
回复
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void UploadButton_Click(object sender, EventArgs e)
{

if (FileUpload1.HasFile)
{

UploadStatusLabel.Text = "Your file is "+FileUpload1.PostedFile.ContentType ;
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload.";
}

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>

<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>

<br /><br />

<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>

<hr />

<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>

  • 打赏
  • 举报
回复
string UpFileType=ImgFile.ContentType.ToLower();
请仔细看我的问题,我这个得到的直永远是:application/octet-stream,永远没有匹配的...所以永远无法判断是不是图片格式

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧