过滤文件上传控件的文件类型

编程入门 行业动态 更新时间:2024-10-21 03:58:41
本文介绍了过滤文件上传控件的文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

例如在点击文件上传控件的浏览按钮,就应该打开浏览文件对话框,只有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,净)和 FileUpload控件很容易去实现它。您可以在Ar​​rayList中allowedExtensions定义文件类型。

I think it 's easy to realise it if you use C# (or VB,net) and 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; }

更多推荐

过滤文件上传控件的文件类型

本文发布于:2023-07-17 03:31:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   文件类型   文件上传

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!