使用文件上传控件过滤文件类型

使用文件上传控件过滤文件类型

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

问题描述

如何用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)和.net fileupload 控件很容易实现它.您可以在数组列表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;
}

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

09-01 22:13