问题
I am trying to upload a picture from the Pictures Library (on WP7) and save it in a folder on the server.
On the server, I'm using PHP to receive the file using POST Method. The PHP Code is:
<?php
$uploads_dir = 'files/'; //Directory to save the file that comes from client application.
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
?>
I've already tried some approaches, but they all just seem to fail. I've already done this job in a Windows Forms application using the Client.UploadFile method but it seems that it cannot be used on a Windows Phone Application.
I think httpwebrequest can help, right?
This is my C# code so far:
public partial class SamplePage : PhoneApplicationPage
{
public SamplePage()
{
InitializeComponent();
}
PhotoChooserTask selectphoto = null;
private void SampleBtn_Click(object sender, RoutedEventArgs e)
{
selectphoto = new PhotoChooserTask();
selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed);
selectphoto.Show();
}
void selectphoto_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BinaryReader reader = new BinaryReader(e.ChosenPhoto);
image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
txtBX.Text = e.OriginalFileName;
}
}
}
I read it somewhere that the image is required to be converted to a string of bytes, I don't know for sure. But, please help me.
Thanks a lot in advance.
回答1:
I would convert the image to base64 (see System.Convert) then transfer via POST:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://mydomain.cc/saveimage.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string postData = String.Format("image={0}", myBase64EncodedImage);
// Getting the request stream.
request.BeginGetRequestStream
(result =>
{
// Sending the request.
using (var requestStream = request.EndGetRequestStream(result))
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(postData);
writer.Flush();
}
}
// Getting the response.
request.BeginGetResponse(responseResult =>
{
var webResponse = request.EndGetResponse(responseResult);
using (var responseStream = webResponse.GetResponseStream())
{
using (var streamReader = new StreamReader(responseStream))
{
string srresult = streamReader.ReadToEnd();
}
}
}, null);
}, null);
}
saveimage.php should look like this:
<?
function base64_to_image( $imageData, $outputfile ) {
/* encode & write data (binary) */
$ifp = fopen( $outputfile, "wb" );
fwrite( $ifp, base64_decode( $imageData ) );
fclose( $ifp );
/* return output filename */
return( $outputfile );
}
if (isset($_POST['image'])) {
base64_to_jpeg($_POST['image'], "my_path_to_store_images.jpg");
}
else
die("no image data found");
?>
Note: I have not tested the code. There might be typos or other errors. It's just to illustrate how I would transfer an image using POST.
Edit as a repy to your comment: I don't have code to encode to base64 at hand but here is how you would decode a base64 encoded image in C#:
byte[] image = Convert.FromBase64String(str);
来源:https://stackoverflow.com/questions/15490347/upload-image-file-in-windows-phone-7-application-to-php