Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not upload image with null Reference exception #11

Open
trktuhin opened this issue Jun 1, 2018 · 0 comments
Open

Can not upload image with null Reference exception #11

trktuhin opened this issue Jun 1, 2018 · 0 comments

Comments

@trktuhin
Copy link

trktuhin commented Jun 1, 2018

My code is completely similar to yours. I have only one issue testing the photo upload api. It seems like my api is not getting a IFormFile object from the body of the request. Here's the proof:
This is the action:
` [HttpPost]
public async Task Upload(int vId,IFormFile fileStream)
{
var vehicle = await this.repository.GetVehicle(vId, hasAdditional: false);
if (vehicle == null)
return NotFound();
var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads");
if (!Directory.Exists(uploadsFolderPath))
Directory.CreateDirectory(uploadsFolderPath);
var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileStream.FileName);
var filePath = Path.Combine(uploadsFolderPath, fileName);

    using (var stream = new FileStream(filePath, FileMode.Create))
    {
        await fileStream.CopyToAsync(stream);
    }

    var photo = new Photo { FileName = fileName };
    vehicle.Photos.Add(photo);
    await unitOfWork.CompleteAsync();
    return Ok(mapper.Map<Photo,PhotoResource>(photo));
}

and the controller with constructor: public class PhotosController : Controller
{
private readonly IHostingEnvironment host;
private readonly IVehicleRepository repository;
private readonly IMapper mapper;
private readonly IUnitOfWork unitOfWork;

    public PhotosController(IHostingEnvironment host, IVehicleRepository repository,IUnitOfWork unitOfWork, IMapper mapper)
    {
        this.mapper = mapper;
        this.repository = repository;
        this.host = host;
}`

error shows on this line:

    var fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileStream.FileName);

which indicates its not getting the "file" object while I am sending an image.jpg with the same key "fileStream".
realerror

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant