Reputation: 7164
I am trying to synchronize binary files (photos) from my Monotouch
application, running on an iPad
, to our PHP
server (Windows) using Webclient
. The files are being sent and received, but the binary files seem to be corrupt on the server and cannot be viewed.
Here is the client side code:
static void UploadPhotos()
{
WebClient client = new WebClient ();
client.Headers.Add("Content-Type","application/octet-stream");
string sUri = GetUri();
client.UploadFile (sUri, "POST", "images/test.png");
}
And here is the PHP
code on the server:
<?php
$uploadDir = "C:\\uploaddir\\";
foreach ($_FILES as $file_name => $file_array) {
$uploadFile = $uploadDir . $file_array['name'];
move_uploaded_file($file_array['tmp_name'], $uploadFile);
}
?>
Does anyone know why the binary data gets corrupted in the upload and how to fix it?
Update:
Very strange indeed. It seems the problem I am having affects png
images only; jpeg
images seem to come across correctly. The jpeg
images show the image dimensions correctly in windows explorer on the server and I am able to preview the jpeg
images. The jpeg
images that I tested were about 90 KB. The png
files though are not coming across correctly. In windows explorer the png
files don't show the dimensions of the images and cannot be previewed. The png
file sizes are larger on the server. So, for example, I use the following png
image:
http://tuxpaint.org/stamps/stamps/animals/birds/cartoon/tux.png
The initial file is 40.2 KB (41236 bytes); after transfer the file size on the server is 45.3 KB (46468 bytes). Anyone got any ideas how this could happen?
Upvotes: 1
Views: 2387
Reputation: 107
I had to use UploadDataAsync
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
byte[] bytes = File.ReadAllBytes(filePath);
client.UploadDataAsync(new Uri(url), bytes);
}
Upvotes: 0
Reputation: 624
Try setting the Content-Type to "application/octet-stream", I think that's more common for binary data and the server may not recognize binary/octet-stream.
Upvotes: 2