所以我正在使用此代码进行查看:
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
这适用于型号:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
除非用户添加不是图像的文件,否则效果很好.如何确保上传的文件是图像.谢谢
我循环遍历目录并复制所有文件.现在我做string.EndsWith检查".jpg"或".png"等..
有没有更优雅的方法来确定文件是否是一个图像(任何图像类型)没有上面的hacky检查?
我不想依赖文件扩展名.我不知道它是什么类型的图像(.jpg,.png等),我只是想知道文件是否是图像.如果可能的话,我宁愿不使用任何非.NET dll.
我知道如何做到这一点的最好方法如下:
bool isImageFile;
try
{
Image.FromFile(imageFile).Dispose();
isImageFile = true;
}
catch (OutOfMemoryException)
{
isImageFile = false;
}
Run Code Online (Sandbox Code Playgroud)
如上所述:http://msdn.microsoft.com/en-us/library/stf701f5.aspx,如果文件不是有效的图像格式,则Image.FromFile()抛出一个OutOfMemoryException.使用上面给出了我想要的结果,但是由于以下原因我不想使用它:
Image.FromFile()将整个图像文件(如果是图像文件)加载到内存中.这是浪费我假设因为我只需要文件类型,并且在我的代码中此时不需要进行任何进一步的图像处理.OutOfMemoryExceptions,因为如果有一个真正的内存不足问题,我的程序吞下它并继续前进?有没有更好的方法来做到这一点?或者,我上面列出的任何/所有问题都是无根据的?
编辑:自从收到答案后,这些是我现在知道的三个解决方案:
Image.FromFile()try-catch 将整个图像加载到内存中.
(我没有看到明确的"赢家",因为我可以想象每个人都适合的情况.对于我的应用程序的目的,文件类型检查不经常发生,方法1的性能问题不是问题.)
我想有一个文件类型的白名单,用户有权上传到我的IIS服务器(我使用IIS v7.5).
我有哪些选择?例如,要将控制器中的特定操作的文件大小限制为5MB,我将此部分添加到我的webconfig:
<location path="home/fileupload">
<system.web>
<!-- maxRequestLength is in kilobytes (KB) -->
<httpRuntime maxRequestLength="5120" /> <!-- 5MB -->
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- maxAllowedContentLength is in bytes -->
<requestLimits maxAllowedContentLength="5242880"/> <!-- 5MB -->
</requestFiltering>
</security>
</system.webServer>
</location>
Run Code Online (Sandbox Code Playgroud)
webconfig中是否有一个选项来设置允许的文件类型的白名单?或者唯一的选择是在文件完全上传时验证代码中的文件类型?什么是推荐的技术?我怎么能确定.docx,.pdf,.jpg等真的是它们是什么?
有没有人知道脚本来验证给定图像的文件格式是什么.目前我正在填充图像对象,查看它的高度,宽度和分辨率.我没有看到解释文件格式的此对象的任何特定属性.
我想查看jpg,AI,PSD,High Jes Jpg,Bitmap和Tiff.
这是我目前的脚本:
protected bool IsValidImage(HttpPostedFileBase file, string fileName) {
//verify that the image is no more than 648 wide and 648 pixels tall
Image imgPhoto = Image.FromStream(file.InputStream);
if (imgPhoto.Width > 648)
return false;
if (imgPhoto.Height > 648)
return false;
if (imgPhoto.HorizontalResolution != 72 || imgPhoto.VerticalResolution != 72)
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
在C#.NET 4.0,基于我下载虚假图像的问题(将错误的aspx页面保存为image.jpg而不是image.jpg中的实际图像),我需要以某种方式读取文件并确定它是否是有效图像.我只需要1个函数public bool IsValidJpgImage(string ImageFilename);返回false的任何东西(不是有效的图像文件)我将从磁盘中删除.
此时上传恶意文件时需要扫描并删除该文件。NPM中有什么特殊的包吗?您能帮我解决这个问题吗?先谢谢了。
c# ×6
image ×5
asp.net-mvc ×2
file ×2
.net ×1
asp.net ×1
corrupt ×1
express ×1
file-format ×1
file-type ×1
file-upload ×1
graphics ×1
iis ×1
jpeg ×1
node.js ×1
razor ×1
validation ×1