Saved ZIP File Gets Corrupted (from An Android App To An IIS Server)
Solution 1:
I would tell the problem is due the conversion of the ZIP File (which is binary) in your code to a UTF-8 stream.
Try replacing the server side code to the following:
string fileName = "D:\\newZIPfile.zip";
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
byte[] buffer = new byte[32768];
int read;
while ((read = Request.InputStream.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write (buffer, 0, read);
}
}
This will write the input stream as received to the ZIP file.
To receive the client ZIP-Filename as well, one possibly way would be to change the line
String urlString = "http://192.168.1.189/prodataupload/Default.aspx";
to something like:
String urlString = "http://192.168.1.189/prodataupload/Default.aspx?CLIENTFILENAME=" +
urlEncodedFilename;
So you can access the parameter by use of the Request.Params
property.
Warning: Do not trust the filename as sent by the client (someone could manipulate it!). Do a strict sanitation/validation on the parameter!
Solution 2:
So this problem has been bugging a lot of people across many forums/threads I have gone through till now.
Here's what I did:
- Change the client side to have binary/octet-stream
content type.
- change the server side code to:
UPDATED:
if(Request.InputStream.Length < 32768) {
Request.ContentType = "binary/octet-stream";
Stream myStream = Request.InputStream;
string fName = Request.Params["CLIENTFILENAME"];
//string fName = Request.Params.GetValues("zipFileName");
int iContentLengthCounter = 0;
int maxlength = (int) myStream.Length;
byte[] bFileWriteData = new byte[maxlength];
string fileName = "D:\\"+ fName +".zip";
//FileStream oFileStream = new FileStream();
while (iContentLengthCounter < maxlength)
{
iContentLengthCounter += Request.InputStream.Read(bFileWriteData, iContentLengthCounter, (maxlength - iContentLengthCounter));
}
System.IO.FileStream oFileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
oFileStream.Write(bFileWriteData, 0, bFileWriteData.Length);
oFileStream.Close();
Request.InputStream.Close();
}
else
{
}
Basically..I have not been picking up data acc. to its length. (answer requires editing..to be done later)
Post a Comment for "Saved ZIP File Gets Corrupted (from An Android App To An IIS Server)"