在我的网络应用程序(asp.net,c#)中我在一个页面上传视频文件,但我只想上传flv视频.我如何限制上传其他扩展视频的时间?
Jam*_*mes 238
string myFilePath = @"C:\MyFile.txt";
string ext = Path.GetExtension(myFilePath);
// ext would be ".txt"
Run Code Online (Sandbox Code Playgroud)
小智 17
您可以只读取文件流
using (var target = new MemoryStream())
{
postedFile.InputStream.CopyTo(target);
var array = target.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
前5/6索引将告诉您文件类型.在FLV的情况下
private static readonly byte [] FLV = {70,76,86,1,5};
private static readonly byte[] FLV = { 70, 76, 86, 1, 5};
bool isAllowed = array.Take(5).SequenceEqual(FLV);
Run Code Online (Sandbox Code Playgroud)
如果是,那么它的FLV.
要么
阅读文件的内容
var contentArray = target.GetBuffer();
var content = Encoding.ASCII.GetString(contentArray);
Run Code Online (Sandbox Code Playgroud)
前两个/三个字母将告诉您文件类型.在FLV的情况下
"FLV |\0\0\0\0\0\0 ......"
content.StartsWith("FLV")
Run Code Online (Sandbox Code Playgroud)
在服务器上,您可以在此处或在Google上检查MIME类型,查找flv mime类型.
你应该检查mime类型是什么
video/x-flv
Run Code Online (Sandbox Code Playgroud)
例如,如果您在C#中使用FileUpload,则可以这样做
FileUpload.PostedFile.ContentType == "video/x-flv"
Run Code Online (Sandbox Code Playgroud)
我不确定这是不是你想要的但是:
Directory.GetFiles(@"c:\mydir", "*.flv");
Run Code Online (Sandbox Code Playgroud)
要么:
Path.GetExtension(@"c:\test.flv")
Run Code Online (Sandbox Code Playgroud)
此外,如果您有FileInfo fi,您可以简单地执行以下操作:
string ext = fi.Extension;
Run Code Online (Sandbox Code Playgroud)
并且它将保存文件的扩展名(注意:它将包括.,因此上述结果可能是:.jpg .txt,依此类推......
小智 6
string FileExtn = System.IO.Path.GetExtension(fpdDocument.PostedFile.FileName);
Run Code Online (Sandbox Code Playgroud)
上述方法适用于 Firefox 和 IE:我可以查看所有类型的文件,如 zip、txt、xls、xlsx、doc、docx、jpg、png。
但是当我尝试从 Google Chrome 中查找文件扩展名时,我失败了。
| 归档时间: |
|
| 查看次数: |
207841 次 |
| 最近记录: |