【问题标题】:asp.net mvc: check content type of the uploaded file 'text/plain' or 'text/csv', I always get application/octet-stream insteadasp.net mvc:检查上传文件'text/plain'或'text/csv'的内容类型,我总是得到application/octet-stream
【发布时间】:2010-12-25 14:40:47
【问题描述】:

我想验证上传文件的类型, 我就是这样做的:

 [AcceptVerbs(HttpVerbs.Post)]
        public ViewResult GetDataFromFile()
        {
            var file = Request.Files.Get(0);
...
            if (file.ContentType != "text/csv")
            {
                ModelState.AddModelError("FileInput.File", "The file uploaded is not in the correct format, please upload a csv file");
                return View("Index", new CandidateBulkInsertIndexInput());
            }
...
        }

但我总是得到 application/octet-stream,有人知道如何检查它是 csv 还是 text MIME 类型?

【问题讨论】:

标签: asp.net-mvc file-upload mime


【解决方案1】:

我猜这个post会回答。

您能否验证用于上传文件的机器是否知道 MIME 类型 text/csv?

还可以尝试验证文件扩展名。

var fileExtension = System.IO.Path.GetExtension(file.FileName);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 2010-11-04
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多