如何将图像从Android上传到asp.net MVC Web服务

时间:2014-06-27 09:53:19

标签: android asp.net-mvc http-post

我必须做一个从我的Android手机拍照并将其发送到asp.net网络服务MVC的应用程序。

这是控制器文件HomeController.cs

public class HomeController : Controller {
    BlobStorageService _blobStorageService = new BlobStorageService();
    public ActionResult Index() {
        return View();
    }

    public ActionResult Upload() {
        CloudBlobContainer blobContainer = _blobStorageService.GetCloudBlobContainer();
        List<string> blobs = new List<string>();
        foreach (var blobItem in blobContainer.ListBlobs())
            blobs.Add(blobItem.Uri.ToString());
        return View(blobs);
    }

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase image) {
        if (image.ContentLength > 0) {
            CloudBlobContainer blobContainer = _blobStorageService.GetCloudBlobContainer();
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(image.FileName);
            blob.UploadFromStream(image.InputStream);
        }
        return RedirectToAction("Upload");
    }

    [HttpPost]
    public string DeleteImage(string Name) {
        Uri uri = new Uri(Name);
        string filename = System.IO.Path.GetFileName(uri.LocalPath);

        CloudBlobContainer blobContainer = _blobStorageService.GetCloudBlobContainer();
        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);

        blob.Delete();

        return "File Deleted";
    }
}

这是视图文件Upload.cshtml

    @{
    ViewBag.Title = "Upload";
}

<h2>Upload Image</h2>

<p>
    @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {
        <input type="file" name="image" />
        <input type="submit" value="Upload" />
    }

</p>

<ul style="list-style-type: none; padding:0;">
    @foreach (var item in Model) {
        <li>
            <img src="@item" alt="images" width="100" height="100" />
            <a id="@item" href="#" onclick="deleteImage('@item')">Delete</a>

        </li>
    }

</ul>

<script>
    function deleteImage(item) {
        var url = "/Home/DeleteImage";
        $.post(url, { Name: item }, function (data) {
            window.location.href = "/Home/Upload";
            alert(data);
        });
    }

</script>

我要做的是创建一个能够做与Web客户端相同的Android客户端。

0 个答案:

没有答案