Click here to Skip to main content
16,021,417 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Could anyone please help me with the following please?

If I have a jpg image on disk, it is easy to post it to a php script and save it server-side as follows:

C#
C#
System.Net.WebClient Client = new System.Net.WebClient();

Client.Headers.Add("Content-Type", "binary/octet-stream");

byte[] result = Client.UploadFile("http://mywebsite.com/UploadImage.php", "POST", "PicData.jpg");

szResult = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);

and the PHP is as follows:

PHP

PHP
<?php

$uploaddir = "uploads/";

$szResult = "";

if (is_uploaded_file($_FILES["file"]["tmp_name"]))
{
    $uploadfile = $uploaddir.basename($_FILES["file"]["name"]);

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $uploadfile))
    {
        $szResult = "Uuploaded successfully.";
    }
    else
    {
        $szResult = "Move to upload folder failed.";
    }
}

else
{
    $szResult = "Upload failed.";
}

echo $szResult;

?>


This all works great.

But, what if I have the jpg file in memory as a byte[] called jpegAsByteArray.

Well then I could do:
C#
File.WriteAllBytes("PicData.jpg", jpegAsByteArray);

And call the above C# code and all will work fine.

But it seams wasteful to save it out to disk just to then send the bytes to the server using UploadFile.

How can I acheive the saving of the file on the server WITHOUT also saving it on the client side?

Thanks,

Mitch.

[Edit]Code block added[/Edit]
Posted
Updated 20-May-13 6:08am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900