2

I am using an third party uploader called Plupload and I wish to pass a selected image to a WebAPI method but I just cant seem to figure out how.

on the front end I Post a file object

uploader.bind('FileUploaded', function (up, file) {

        data = { name: file.name, id: file.id, size: file.size, key: path };

        $.ajax({
            url: sf.getServiceRoot('mysite') + "upload/getS3Client",
            type: "POST",
            data: data,
            beforeSend: sf.setModuleHeaders
        }).done(function (response, status) {
            alert(response);
        }).fail(function (xhr, result, status) {
            alert("error: " + result);
        });
    });

on the back end I receive the file object but I don't know what to do with it...

there is a MVC code snippet out there that I tried to use but apparently Web API does not support Request.Files[0] so I am lost. this is the snippet

 HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength > 0) // accept the file
{
                string accessKey = "XXXXXXXXXXX";
                string secretKey = "122334XXXXXXXXXX";
                AmazonS3 client;

   using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
     {
          PutObjectRequest request = new PutObjectRequest();
          request.WithBucketName("mealtik")
         .WithCannedACL(S3CannedACL.PublicRead)
         .WithKey("meals/test.jpg").InputStream = file.InputStream;
        S3Response response = client.PutObject(request);
     }
}

my method looks like this..its in flux right now.

public HttpResponseMessage getS3Client(file submitted)
    {
        HttpRequestMessage request = this.Request;

            string accessKey = "xxxx";
            string secretKey = "xxxx";
            AmazonS3 client;
        using (client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey ))
        {
           MemoryStream ms = new MemoryStream();
           PutObjectRequest request = new PutObjectRequest();
           request.WithBucketName("Dev");
           .WithKey("drop/" + submitted.name).InputStream = file.InputStream;
           S3Response response = client.PutObject(request);
        }


        return Request.CreateResponse(HttpStatusCode.OK, "Success");
    }

to sum it up. I am passing the file object to my method and trying to push it up to amazon s3 the stated MVC method work as it would seem but the same does not apply for Web API.

How might I do this?

Mark Hollas
  • 1,107
  • 1
  • 16
  • 44
  • If I am not missing something this appears to be a duplicate of/ highly related to http://stackoverflow.com/questions/13074791/upload-a-file-mvc-4-web-api-net-4 – Mark Jones Jan 28 '13 at 08:52
  • I have seen that code snippet as well an do not understand it either. I do not see where the file is being obtained from. It is not as cut and dry as the the MVC Request.Files[0] so because of that I am lost as to how I could implement even that snippet to my current project. – Mark Hollas Jan 28 '13 at 08:58

0 Answers0