How to upload an image from Android to asp.net MVC web service -
i have application take picture android phone , send asp.net web service mvc.
here controller file 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"; } }
and view file 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>
what have create android client able same things web client does.
Comments
Post a Comment