User-1336361068 posted
Hi there
I am trying to utilise the API that issuu.com provide to upload a file to their website.
If I upload a small pdf file (200Kb) the process works fine. However when I try and upload a larger file (5Mb) I get an error message back from issuu.com that the required field "_uploadFilename" is missing.
So I am wondering if my code isn't reading the file correctly/completely for the larger file and therefore I am not sending the required data.
My code is below for attaching the data and the pdf file to my HTTPWebRequest.
private static Stream GetPostStream(string fullFilePath, NameValueCollection formData, string boundary)
{
Stream postDataStream = new System.IO.MemoryStream();
//adding form data
string formDataHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine +
"Content-Dis-data; name=\"{0}\";" + Environment.NewLine + Environment.NewLine + "{1}";
foreach (string key in formData.Keys)
{
byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(formDataHeaderTemplate, key, formData[key]));
postDataStream.Write(formItemBytes, 0, formItemBytes.Length);
}
//adding file data
FileInfo fileInfo = new FileInfo(fullFilePath);
string fileHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine + "Content-Dis-data; name=\"{0}\"; filename=\"{1}\"" + Environment.NewLine + "Content-Type: application/pdf" + Environment.NewLine + Environment.NewLine;
byte[] fileHeaderBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(fileHeaderTemplate, "file", fileInfo.FullName));
postDataStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
FileStream fileStream = fileInfo.OpenRead();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
postDataStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--");
postDataStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
return postDataStream;
}
Can anyone provide any feedback.
Thanks
Nigel