2

I am uploading an image from my android device to my ASP.NET MVC server.

The save path I would like to use is HttpContext.Server.MapPath("../") + "App_Data/" + HttpContext.User.Identity.Name + model.Firstname + "-" + model.Surname + "-" + System.IO.Path.GetFileName(Request.Files[0].FileName);

Which should give /App_Data/AccountName/firstname-surname-filename.jpg

This works for me when I do this within the MVC itself using

@Html.LabelFor(m => m.Firstname)
@Html.TextBoxFor(m => m.Firstname)            

@Html.LabelFor(m => m.Surname)
@Html.TextBoxFor(m => m.Surname)            

<label for="file1">Upload Image</label>
<input type="file" name="picture" id="picture" />      

<input type="submit"  />

In order to execute the upload from the Android Application I need to use javascript from within the MVC as follows

<script type="text/javascript">
function showAndroidToast(toast) 
{
    Android.showToast(toast);
}
</script> 
<input type="submit" value="Scan Fingerprint" onClick="showAndroidToast('Scanning fingerprint')" />'

Where the showToast function is the upload function in my android application.

I can get the image to upload from the android but even though I have logged into my ASP.NET application, the save path doesn't work for the HttpContext.User.Identity.Name which I need to work at the bare minimum. If I add [Authorize] to my upload controller as well, the upload won't work which further suggests it isn't using the log-in information.

Is it possible to do this when uploading from an Android device to the ASP.NET web service?

1 Answer 1

1

Since the image upload is performed on a new thread (JavascriptInterface) you will need to somehow recognize the image being uploaded by a means of a token or specific file name. From this you can 'search' for the file from within your MVC after the file has been uploaded. Once it catches that the file has been uploaded, it may then rename or move the file to the specified folder of your choosing using HttpContext.User.Identity.Name etc.

For example I would create a random string (ndNk3KasL.jpg), pass this with the upload trigger and have the file named this. Upon receiving the upload request, I would check to see if it is the correct file name ndNk3KasL and then rename/move it if it is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.