Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.net Core文件中设置默认映像_C#_Asp.net Core_Model View Controller - Fatal编程技术网

C# 如何在ASP.net Core文件中设置默认映像

C# 如何在ASP.net Core文件中设置默认映像,c#,asp.net-core,model-view-controller,C#,Asp.net Core,Model View Controller,我需要设定一个条件,如果用户不想上传图像(IFormFile文件),那么我可以从控制器设置默认图像。我怎样才能做到这一点?我的代码如下: if (file == null) { var fullPath = @"C:\svnProjectos345\sClubeWebEstagio\trunk\sClube\wwwroot\images2\t.png"; using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate

我需要设定一个条件,如果用户不想上传图像(IFormFile文件),那么我可以从控制器设置默认图像。我怎样才能做到这一点?我的代码如下:

if (file == null)
{
    var fullPath = @"C:\svnProjectos345\sClubeWebEstagio\trunk\sClube\wwwroot\images2\t.png";

    using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate))
    {

        await file.CopyToAsync(fs);
    }
    using (var ms = new MemoryStream())
    {
        file.CopyTo(ms);
        var fileBytes = ms.ToArray();
        string s = Convert.ToBase64String(fileBytes);
        person.ImagePath = s;
    }
    _context.Update(person);
    await _context.SaveChangesAsync();
}
型号

public IFormFile Foto { get; set; }

public string ImagePath { get; set; 
控制器

public async Task<IActionResult> Create(Person person,IFormFile file)
{

    if (ModelState.IsValid)
    {
        if (person.BirthYear == 0)
        {
            person.BirthYear = person.BirthDate.Value.Year;
        }


        if (file != null && file.Length > 0)
        {
            var ImagePath = @"/images2/";
            var uploadPath = _env.WebRootPath + ImagePath;

            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            var uniqFileName = Guid.NewGuid().ToString();
            var filename = Path.GetFileName(uniqFileName + "." + file.FileName.Split(".")[1].ToLower());
            string fullPath = uploadPath + filename;

            ImagePath = ImagePath + @"\";
            var filePath = @".." + Path.Combine(ImagePath, filename);

            using (var fileStream = new FileStream(fullPath, FileMode.Create))
            {

                await file.CopyToAsync(fileStream);
            }
            using (var ms = new MemoryStream())
            {
                file.CopyTo(ms);
                var fileBytes = ms.ToArray();
                string s = Convert.ToBase64String(fileBytes);
                person.ImagePath = s;
            }
            _context.Add(person);
            await _context.SaveChangesAsync();

            ViewData["FileLocation"] = filePath;

        }
        return RedirectToRoute(new { controller = "People", action = "Details", id = person.PersonId });
    }


    return View(person);
}

但是我从wait file.CopyToAsync(fs);,得到错误(对象引用未设置为对象的实例),有人能帮我吗?ty

你写了一个if语句

if(file == null){......
你想这么做吗

await file.CopyToAsync(fs);
 if(file != null){......
它无法工作,因为对象文件为空

也许你想这么做

await file.CopyToAsync(fs);
 if(file != null){......
试试这个:

if (file == null)
{                    
   //be sure your file path is right
    var fileBytes = System.IO.File.ReadAllBytes(@"C:\svnProjectos345\sClubeWebEstagio\trunk\sClube\wwwroot\images2\t.png");
    string s = Convert.ToBase64String(fileBytes);
    person.ImagePath = s;
    _context.Update(person);
    await _context.SaveChangesAsync();
}