首页 > 解决方案 > 看起来我们不支持这种文件格式

问题描述

我要上传图片但不支持文件格式我不知道是什么问题。文件将被上传,但格式改变了什么是不能在照片查看器中浏览的问题。

<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();

    }

有人能帮助我吗

标签: c#-4.0

解决方案


试试看。

    [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("");
    }

推荐阅读