zoukankan      html  css  js  c++  java
  • 取得文件真正扩展名类型

    实现这个功能,需要使用一个System.IO名称空间。只需判断文件流前两个字节即可。

    参考代码:

    View Code
     string GetFileCass(string path)
        {
            
    try
            {
                FileStream fs 
    = new FileStream(path, FileMode.Open, FileAccess.Read);
                BinaryReader reader 
    = new BinaryReader(fs);
                
    string fileClass = string.Empty;
                
    byte buffer;

                
    byte[] b = new byte[2];
                buffer 
    = reader.ReadByte();
                b[
    0= buffer;
                fileClass 
    = buffer.ToString();
                buffer 
    = reader.ReadByte();
                b[
    1= buffer;
                fileClass 
    += buffer.ToString();
                reader.Close();
                fs.Close();

                
    return fileClass;
            }
            
    catch
            {
                
    return string.Empty;
            }
        }

    例子演示:

    <asp:FileUpload ID="FileUpload1" runat="server" /><br />    
        
    <asp:Button ID="Button1" runat="server" Text="Get File Extension Info" onclick="Button1_Click" /><br />
        
    <p></p>
        
    <asp:Label ID="lblExtension" runat="server" Text=""></asp:Label><br />
        
    <asp:Label ID="lblFileClass" runat="server" Text=""></asp:Label>

    按钮事件:

    View Code
     protected void Button1_Click(object sender, EventArgs e)
        {
            
    if (!File.Exists(this.FileUpload1.PostedFile.FileName))
            {
                
    //follow Js class, download address:http://www.cnblogs.com/insus/articles/1341703.html
                Insus.NET.InsusJsUtility objJs = new Insus.NET.InsusJsUtility();
                objJs.JsAlert(
    "You did not specify a file.");
                
    return;
            }

            
    string path = this.FileUpload1.PostedFile.FileName;
            
    this.lblExtension.Text ="Extension: "+ path.Substring(path.LastIndexOf("."));
            
    this.lblFileClass.Text = "FileClass: " +  GetFileCass(path);
        }

    选择一个Excel文件得到的结果:

  • 相关阅读:
    iphone开发 使用TouchJSON框架 解析JSON
    iphone开发UIScrollView控件详解
    iPhone UIAlertView属性及使用方法
    vim文本编辑器使用大全 命令的解读
    iphone开发软件Xcode3.2.6破解免证书真机开发调试方案
    Objectivec语言 字符串类NSMutableString用法
    iphone开发 如何在NSMutableDictionary中放入基本数据类型
    找了很久,终于让我找到了,登陆界面登陆按钮随着输入法键盘的弹出而动态改变
    微软安全指南中心:Windows 2000
    在网络中安装、配置和使用SUS服务
  • 原文地址:https://www.cnblogs.com/insus/p/1980495.html
Copyright © 2011-2022 走看看