2

我正在使用名为 Plupload 的第三方上传器,我希望将选定的图像传递给 WebAPI 方法,但我似乎无法弄清楚如何。

在前端我发布一个文件对象

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);
        });
    });

在后端我收到文件对象,但我不知道如何处理它......

我尝试使用一个 MVC 代码片段,但显然 Web API 不支持 Request.Files[0] 所以我迷路了。这是片段

 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);
     }
}

我的方法看起来像这样..它现在在不断变化。

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");
    }

把它们加起来。我将文件对象传递给我的方法并尝试将其推送到 amazon s3,所述 MVC 方法似乎可以正常工作,但同样不适用于 Web API。

我该怎么做?

4

0 回答 0