1

I have Mobile applications i.e android, IPhone based in which I have to upload image, on server side I am using asp.net , web service ".asmx". Now I don't know how to receive file sent by any of the mobile application, or in simple words what I have to do at server side to get that file and store at my local system? I have tried the following

  • I have created a asp.net page where i have placed control and posted file using multipart/form-data , but app developer wants me to give them url with a parameter that will receive file name and then upload it to server.

But how to achieve it ?

4
  • What code have you tried? What errors are you getting? Commented Nov 30, 2015 at 10:27
  • using that page i am successfully uploading file if using on browser, but app developers want a url with file name parameter and then upload the file to the server that is what i am unable to understand how to achieve this? Commented Nov 30, 2015 at 10:29
  • or someone can explain me what steps do i have to take if i have to provide a link or url to mobile app developer through which he can upload a file or image on server? Commented Nov 30, 2015 at 10:32
  • Fixed some grammer issues Commented Dec 2, 2015 at 11:15

1 Answer 1

1

This is how I achieved File Upload using WebMethod , both android & Apple applications used this WebMethod for uploading files on server, I tested it using google chrome extension Advanced rest client for reading more about rest API Client Click Here

[WebMethod]
public void UploadFile()
{
    FileResponse master = new FileResponse();
    //HTTP Context to get access to the submitted data
    HttpContext postedContext = HttpContext.Current;
    //File Collection that was submitted with posted data
    HttpFileCollection Files = postedContext.Request.Files;
    string sessionId = Server.UrlDecode((string)postedContext.Request.Form["sessionId"]);
    master.sessionId = sessionId;
    master.timeStamp = DateTime.Now.DdMonYyyy_HhMmSs();
    master.status = Status.Failure;
    master.sessionStatus = Status.Failure;
    master.message = "Image not found";
    try
    {
        if (isGuest_Employee(sessionId))
        {
            //Make sure a file was posted
            string fileName = string.Format("feedbackImgSign_{0}.png", Helper.LDFileNumberSeq);
            if (Files.Count == 1 && Files[0].ContentLength >= 1)
            {
                //The byte array we'll use to write the file with
                byte[] binaryWriteArray = new
                byte[Files[0].InputStream.Length];
                //Read in the file from the InputStream
                Files[0].InputStream.Read(binaryWriteArray, 0,
                (int)Files[0].InputStream.Length);
                //Open the file stream
                FileStream objfilestream = new FileStream(Helper.UploadedFilesDirectory + "\\" +
                fileName, FileMode.Create, FileAccess.ReadWrite);
                //Write the file and close it
                objfilestream.Write(binaryWriteArray, 0,
                binaryWriteArray.Length);
                objfilestream.Close();

                master.FILE_NAME = fileName;
                master.status = Status.Success;
                master.sessionStatus = Status.Success;
                master.message = "Image uploaded successfully!";
            }
        }
        else
        {
            master.status = Status.Failure;
            master.sessionStatus = Status.Failure;
            master.message = "Unable to authenticate provided session Id";
        }
    }
    catch (Exception ex)
    {
        master.status = Status.Failure;
        master.message = ex.Message;
    }

   string strJSON = JsonConvert.SerializeObject(master, Formatting.Indented,
           new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });

    Context.Response.Clear();
    Context.Response.ContentType = "application/json";
    Context.Response.Write(strJSON);
}
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.