过滤文件上传控件的文件类型 [英] filter the file type with the file upload control

查看:418
本文介绍了过滤文件上传控件的文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何筛选在asp.net&放文件上传控件的文件类型; C#.NET

how to filter the file type with the file upload control in asp.net & c#.net

例如在点击文件上传控件的浏览按钮,就应该打开浏览文件对话框,只有excel文件类型。

for example on clicking the browse button of the file upload control ,it should open browse file dialog with only excel file types.

这怎么可能。

推荐答案

这是从其他论坛的答案

我觉得如果你使用C#(或VB,净)和.net FileUpload控件很容易去实现它。您可以在Ar​​rayList中allowedExtensions定义文件类型。

I think it 's easy to realise it if you use C# (or VB,net) and .net fileupload control. you may define file types in arraylist "allowedExtensions".

string upload_Image(FileUpload fileupload, string ImageSavedPath)
{
    FileUpload fu = fileupload;  
    string imagepath = "";
    if (fileupload.HasFile)
    {
        string filepath = Server.MapPath(ImageSavedPath);  
        String fileExtension = System.IO.Path.GetExtension(fu.FileName).ToLower();
        String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
        for (int i = 0; i < allowedExtensions.Length; i++)
        {
            if (fileExtension == allowedExtensions[i])
            {
                try
                {
                    string s_newfilename = DateTime.Now.Year.ToString() +
                        DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() +                           DateTime.Now.Second.ToString() +  fileExtension; 
                        fu.PostedFile.SaveAs(filepath + s_newfilename);

                   imagepath = ImageSavedPath + s_newfilename;
               }
               catch (Exception ex)
               {
                   Response.Write("File could not be uploaded.");
               }

           }

       }

   }
   return imagepath;
}

这篇关于过滤文件上传控件的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆