加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用文件上传控件过滤文件类型

发布时间:2020-12-15 03:58:25 所属栏目:百科 来源:网络整理
导读:如何使用文件上传控件在asp.net C#.NET 例如,点击文件上传控件的浏览按钮,应该打开只有excel文件类型的浏览文件对话框. 这怎么可能 解决方法 这是另一个论坛的答案 如果您使用C#(或VB,net)和.net fileupload控件,我觉得很容易实现.您可以在arraylist“allowe
如何使用文件上传控件在asp.net& C#.NET

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

这怎么可能

解决方法

这是另一个论坛的答案

如果您使用C#(或VB,net)和.net fileupload控件,我觉得很容易实现.您可以在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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读