'Upload file to core hosted server returns 'Object reference not set to an instance of an object.'
I'm trying to upload a file to the server, as I have already done in my project. However, I keep getting the Object reference not set to an instance of an object error on catch.
I've copied over the exact code I've used to upload another file, and checked that the file content is there when pushing it to the server. But I can't even hit any of my breakpoints on the server.
private void OnInputFileChanged(InputFileChangeEventArgs e)
{
_files = new List<IBrowserFile>();
var files = e.GetMultipleFiles();
fileNames = files.Select(f => f.Name).ToList();
foreach (var file in files)
{
if (file is not null)
{
_filename = file.Name;
_files.Add(file);
}
}
}
private async Task Upload()
{
if (_files.FirstOrDefault() is null)
return;
_loading = true;
var file = _files.First();
using (var ms = file.OpenReadStream(file.Size))
{
var content = new MultipartFormDataContent();
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data");
content.Add(new StreamContent(ms, Convert.ToInt32(file.Size)), "csv", _filename);
Interceptor.RegisterEvent();
await Repository.AddMultiAsset(content, ClientUser.Email);
Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
Snackbar.Add("Files uploaded successfully!", Severity.Success);
_files.Clear();
fileNames.Clear();
}
_loading = false;
}
And then the Repo handling api calls:
public async Task AddMultiAsset(MultipartFormDataContent content, string user)
{
try
{
using (var response = await _client.PostAsync($"api/admin/upload?user={user}", content))
{
response.EnsureSuccessStatusCode();
};
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
throw;
}
}
The catch is where I get the 'Object reference not set to an instance of an object.' error, although I know the httpclient, user variable, or MultipartFormDataContent content are all not null.
I can't see any network calls in the dev tools either so it's not even making a request to the server.
I'm at a lost as to what to do here, I have another function exactly the same just pointing to a different controller that works perfectly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
