c#-4.0 - 看起来我们不支持这种文件格式
问题描述
我要上传图片但不支持文件格式我不知道是什么问题。文件将被上传,但格式改变了什么是不能在照片查看器中浏览的问题。
<form class="wizard-form steps-validation" enctype="multipart/form-data" asp-controller="Application" method="post" data-fouc>
<fieldset>
<div class="row">
<div class="col-md-6">
<label class="d-block"> Photo <span class="text-danger">*</span></label>
@*<input type="file" id="cropimag" class="form-input-styled" asp-for="Photo.FileName" accept=".jpg,.png,.gif" data-fouc required="" />*@
<label class="label" data-toggle="tooltip" title="Upload your photo">
<img style="height:213px;" class="rounded" id="avatar" src="~/Images/profile.png" alt="avatar">
<input type="file" class="sr-only" id="input" asp-for="Photo.FileName" accept="image/*" required="">
@* <input type="file" name="name" asp-for="Photo.FileName">*@
</label>
</div>
C#代码在这里
var NewPhoto = new Models.Photo
{
FileName = request.Model.Photo.FileName
};
string ImagePath = NewPhoto.FileName;
string[] pathArr = ImagePath.Split('\\');
string Filename = pathArr.Last().ToString();
if (Filename.Length > 0 & Filename.Length <= 1500)
{
var FileName = Filename.ToLower();
string ext = Path.GetExtension(FileName);
if (ext == ".png" || ext == ".jpg" || ext == ".gif")
{
var filename = Filename;//DateTime.Now.ToFileTime() +Filename
var RootPath = _hostingEnvirnment.ContentRootPath;
var FolderPath = Path.Combine("wwwroot", "Photo");//FolderPath
var fullpath = Path.Combine(RootPath, FolderPath);
var SavePath = Path.Combine(fullpath, filename);
using (var strema = new FileStream(SavePath, FileMode.Create, FileAccess.Write))
{
NewPhoto.FileName = filename;
NewPhoto.ContentType = ext;
NewPhoto.Path = RootPath;
NewPhoto.Root = FolderPath;
NewPhoto.ProfileId = newProfileId;
}
}
}
控制器在这里
[HttpPost]
public async Task<IActionResult> Create([FromBody]LookupsViewModel lookupsViewModel)
{
var datacommand= new CreateApplicationCommand
{
Model = lookupsViewModel
};
await Mediator.Send(datacommand);
var storage = new LocalStorage();
var profileId = storage.Get("getid");
return View();
}
有人能帮助我吗
解决方案
试试看。
[AllowAnonymous]
[HttpPost]
public async Task<string> UploadPhoto(IFormFile Photo)
{
if (Photo != null)
{
VirusScanResult scanResult = _virusScanner.ScanFile(Photo).Result;
if (scanResult == null)
return JsonConvert.SerializeObject("Image scanning failed");
else if (scanResult.Positives > 0 && scanResult.Scans.Count > 0)
{
return JsonConvert.SerializeObject(new { Result = "Image is infected file", ScanResult = scanResult.Scans });
}
User user = null;
if (User != null)
{
user = await _unitOfWork.Users.GetUserAsync(User);
}
string imagesDirectory = "images";
//Checking type of uploading (to temporaryImages or images folder)
StringValues values;
if (!Photo.Headers.TryGetValue("SaveType", out values))
{
imagesDirectory = "temporaryImages";
}
Dictionary<string, string> response = new Dictionary<string, string>();
//===== IMG Upload ======
string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, imagesDirectory);
string uniqueFileName = Guid.NewGuid().ToString() + "_" + Photo.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
var fileStream = new FileStream(filePath, FileMode.Create);
Photo.CopyTo(fileStream);
fileStream.Dispose();
//=======================
var photoUrl = uniqueFileName;
response.Add("photoUrl", photoUrl);
if (user != null)
{
response.Add("oldImageUrl", user.PhotoUrl);
user.PhotoUrl = photoUrl;
await _unitOfWork.Users.Update(user);
await _unitOfWork.CompleteIdentity();
}
return JsonConvert.SerializeObject(response);
}
return JsonConvert.SerializeObject("");
}
推荐阅读
- flutter - 如何在 Flutter 中以这种格式从 API 中获取数据
- java - java : Coverity - 锁定对象的错误选择
- r - 在数据框上运行 summary() 函数时,分类变量未正确汇总
- android - HMS Weather Awareness 中的 getAqi() 始终返回空值
- python - Django context_processor 没有名为的模块
- python - 卸载的 wrapt 模块:Python 不工作
- xcode - 是否可以创建独立的 Quick Look 扩展/插件?
- angular - 如何在 Angular 中使用 multipart/form-data 签署 AWS 请求?
- windows - 如何使批处理文件在继续之前等待另一个调用的所有进程完成
- c# - 如何将数据插入 EF Core 中的关系表