This project is read-only.

Upload image via POST - HttpPostFile error

Sep 13, 2013 at 5:59 PM
hi, i am fairly new to wp/silverlight, and i thank you for having this nice library.

i want to upload a file, which i want to pass to a PHP script.
this is what i use:
 request.Data.Add("title", "mytoolkittitle"); // POST data
 request.Data.Add("story", "mytoolkitstory"); 
 request.Files.Add(new HttpPostFile("file", "random.jpg", filepath)); // POST files
 request.ConnectionTimeout = 30;
 Http.Post(request, OnRequestFinished);
the filepath is a string made of this, which also consists of the fileNAME after choosing a photo via photoChooserTask. is is correct that the filename is included?
filepath = e.OriginalFileName; 
after i try an upload, the debugger points me to this, with a popup which says "UnauthorizedAccessException":
if (!response.Canceled)
                {
                    // display exception
                    MessageBox.Show(response.Exception.Message);
                }
What do I need to consider with the paramters of "HttpPostFile"?

Thank you
muri
Sep 13, 2013 at 6:07 PM
You cannot directy access this file, you have to copy it first to the local storage like this:

var task = new PhotoChooserTask();
        task.ShowCamera = true; 
        task.Completed += (s, r) =>
        {
            if (r.TaskResult == TaskResult.OK)
            {
                try
                {
                    var ext = Path.GetExtension(r.OriginalFileName);
                    var filename = "/Shared/reporter_image" + ext;
                    var isf = IsolatedStorageFile.GetUserStoreForApplication();
                    using (var stream = new IsolatedStorageFileStream(filename, FileMode.Create, FileAccess.Write, isf))
                    {
                        var bytesInStream = new byte[r.ChosenPhoto.Length];
                        r.ChosenPhoto.Read(bytesInStream, 0, bytesInStream.Length);
                        stream.Write(bytesInStream, 0, bytesInStream.Length);
                    }
                    r.ChosenPhoto.Close();
                    // todo: use filename here for http file
                }
                catch
                {

                }
            }
        };
        task.Show();
Sep 13, 2013 at 6:08 PM
alright, i will look into it! thanks :)
Sep 13, 2013 at 6:31 PM
Edited Sep 13, 2013 at 6:32 PM
request.Files.Add(new HttpPostFile("name", "file.jpg", path/to/file.jpg)); // POST files
can u give me an example of THIS line with actual data?
the "name" is (i think) the name of the form-field-name. (<input name="name">) right?
"file.jpg" (i think) can be whatever i want, its the name of the file on the server, right?
and what aber the filepath?

in your example rsuter, /Shared/reporter_image.jpg would be the filepath.
Sep 13, 2013 at 7:42 PM
i made it work! :) with isolated storage!
but there was another problem. my PHP script only allowed types of "image/jpeg, png" etc.
but the file-type was "application/octet-stream", so I allowed this type in my php script as well.

wish i could tell my php script that it is always an image/jpg type, but i think its just fine as it is.
Sep 13, 2013 at 8:27 PM
I think you can change the content type on the httppostrequest..