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

FileUpload error: "Error while copying content to a stream" #17

Open
KerryXEX opened this issue Aug 27, 2023 · 6 comments
Open

FileUpload error: "Error while copying content to a stream" #17

KerryXEX opened this issue Aug 27, 2023 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@KerryXEX
Copy link

Hi,

I'm getting the following exception when trying to upload a file:
Unhandled exception rendering component: Error while copying content to a stream.
System.Net.Http.HttpRequestException: Error while copying content to a stream.
---> System.ObjectDisposedException: Safe handle has been closed.
Object name: 'SafeHandle'.
at Interop.Sys.PRead(SafeHandle fd, Byte* buffer, Int32 bufferSize, Int64 fileOffset)
at System.IO.RandomAccess.ReadAtOffset(SafeFileHandle handle, Span`1 buffer, Int64 fileOffset)
at Microsoft.Win32.SafeHandles.SafeFileHandle.ThreadPoolValueTaskSource.ExecuteInternal()

The project is .NET7 Blazor Server and same error on console app on macos.
I can login correctly, list all shares, but the upload raises this error. Local file name is correct, target share+folder is correct and user has write access.

The request reads:
var uploadResult = await client.FileStationApi().UploadEndpoint().UploadAsync("/Users/Kerry/Desktop/test.pdf", "/System/Upload", true);

What else could I check?
Thanks
Kerry.

@KerryXEX
Copy link
Author

Ok, I could resolve it by using byte[] instead of the filename to upload.
For text files and MS office files it worked with filename upload, but not for binary files like pdf or avi.

Maybe the filename upload function should also just read the file and convert to byte[] ?

Cheers
Kerry.

@plneto
Copy link
Owner

plneto commented Aug 28, 2023

Thanks for reporting this issue.

Currently it loads the file using File.OpenRead(filePath). Do you have any suggestions on how to change it?

@plneto plneto added the help wanted Extra attention is needed label Aug 28, 2023
@KerryXEX
Copy link
Author

Actually this should also work and is described as more flexibal and testable.
However, I have often used the following without issues with binary files:
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))

@plneto
Copy link
Owner

plneto commented Sep 15, 2023

I've published a new version that uses File.ReadAllBytes(filePath) that should fix the issue. Can you please try again?

@KerryXEX
Copy link
Author

Unfortunately I can't really test it because now I get a JSON parse error for any action that returns something:

Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.Int32. Path: $.data.folders[0].additional.owner.gid | LineNumber: 0 | BytePositionInLine: 60.
---> System.FormatException: Either the JSON value is not in a supported format, or is out of bounds for an Int32.
at System.Text.Json.ThrowHelper.ThrowFormatException(NumericType numericType)
at System.Text.Json.Utf8JsonReader.GetInt32()
at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonCollectionConverter2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value) at System.Text.Json.Serialization.JsonConverter1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) --- End of inner exception stack trace --- at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex) at System.Text.Json.Serialization.JsonConverter1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.ContinueDeserialize[TValue](ReadBufferState& bufferState, JsonReaderState& jsonReaderState, ReadStack& readStack, JsonTypeInfo jsonTypeInfo)
at System.Text.Json.JsonSerializer.ReadFromStreamAsync[TValue](Stream utf8Json, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
at System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsyncCore[T](HttpContent content, Encoding sourceEncoding, JsonSerializerOptions options, CancellationToken cancellationToken)
at Synology.Api.Client.SynologyHttpClient.HandleSynologyResponse[T](HttpResponseMessage httpResponse, IApiInfo apiInfo, String apiMethod)
at Synology.Api.Client.SynologyHttpClient.GetAsync[T](IApiInfo apiInfo, String apiMethod, Dictionary`2 queryParams, ISynologySession session)
at Program.synoTest() in /Users/Kerry/Dev/ZEPassess/FirebirdTest/Program.cs:line 125
at System.Threading.Tasks.Task.<>c.b__128_1(Object state)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
at System.Threading.Thread.StartCallback()

The commands (like login or folder creation) are executed, but there seems to be a problem with the returned json parsing.

I'm on latest .net7 environment.

@plneto
Copy link
Owner

plneto commented Sep 23, 2023

Would you be able to capture the returned JSON and send it to me?

I'm unable to reproduce the error. I've added a sample .NET 7 console application to the repository which works fine for me.

I've also published a new version but I only fixed some of the NRT warnings so it probably won't make much difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants