Error when uploading a file.
Error:
System.Net.ProtocolViolationException: The contentlength byte must be written to the request stream before calling [Begin]getresponse.
Cause: The length of the pass is inconsistent with the contentlength you write in the header file.
Workaround:
Ensure that the contentlength is consistent with the transmitted data length.
Instance:
FileStream filestream= New FileStream (FilePath, FileMode.Open, FileAccess.Read, Fileshare.readwrite))
byte[] Sendstreamdata = Streamtobyte (signfile);//Transfer files into byte[]
Post (FileStream);//This function is used to transfer the file stream, here will be an error, because the current stream's initial position is the end position.
The reason I showed this file is that the file stream has been read by me and has not been repositioned.
Plus the positioning code, problem solving.
Modify the following:
FileStream filestream= New FileStream (FilePath, FileMode.Open, FileAccess.Read, Fileshare.readwrite))
byte[] Sendstreamdata = Streamtobyte (signfile);//Transfer files into byte[]
Filestream.seek (0, Seekorigin.begin);//position to initial position. The new Code
Post (FileStream);//This function is used to transfer file streams,
Upload file Error System.Net.ProtocolViolationException: The contentlength byte must be written to the request stream before calling [Begin]getresponse.