我的代码是这样的:
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"
这是为什么?请高手帮忙!!!

解决方案 »

  1.   

    为什么不写成一页呢??a.aspx又不是静态页面。呵呵这个是我写的了。还是看看对你有帮助么? 主要是通过string来处理filename了
    public string ExcuteUpLoad(HtmlInputFile upPhoto,string remote)
    {
    try
    {
    if(upPhoto.PostedFile.FileName!=string.Empty)
    {
    string oldpath = upPhoto.PostedFile.FileName;
    ///截取文件名 
    string fileName = oldpath.Substring(oldpath.LastIndexOf(@"\")+1);
    ///获取服务端的物理目录 remote= Server.MapPath(@".\upload\");
    string remotePath = remote;
    ///截取扩展名
    string fileExtension = oldpath.Substring(oldpath.LastIndexOf("."));
    ///使用当前“年月日时分秒”生成随机文件名
    string newName = GetUniqueString()+fileExtension;
    ///服务端保存的路径
    string savedpath = remotePath+newName;
    ///可以自定义检查文件扩展名,不符合要求就,中止上传,返回空值了 
    if(!checkExt(fileExtension))
    {
    return string.Empty;
    }
    ///检查文件的大小,超过了就中止上传返回空值
    if(checkSize(upPhoto.PostedFile.ContentLength))
    {
    return string.Empty;
    }
    ///保存上传
    upPhoto.PostedFile.SaveAs(savedpath);
    ///返回新的文件名
    return newName; 
    }
    }
    catch(Exception ee)
    {
    throw(ee);
    }
    return string.Empty;
    }
    public string GetUniqueString()
    {
    return DateTime.Now.ToString("yyyymmddhhmmss");
    }///文件扩展名可以自己定制了
    private bool checkExt(string FileExt)
    {
    string [] arrExt = {".jpg",".jpeg",".gif",".png"};
    for(int i=0;i<arrExt.Length;i++)
    {
    if(FileExt.Equals(arrExt[i]))
    return true;
    }
    return false;
    }
    ///限制200的大小。或者可以自己传一个参数定制i的大小了
    private bool checkSize(int fileLength)
    {
    int i = 200;
    if(fileLength>i*1024)
    return true;
    return false;
    }
      

  2.   

    由于项目的设计:a1,a2,a3多个页面提交给b页.因此不能写成一页.否则我肯定写一页了.