Image not getting uploaded

FOJI PETMART 1 Reputation point
2021-08-23T14:04:59.197+00:00

Not able to upload image from my App. I am using an api do upload the image through mobile but getting error all time . The same piece of code is working fine in Bigrock Server and on my local too . I am Uploading the image as body Multipart . I am giving u the Url , error msg and image uploading api code below.

URL -- https://onpetapi.azurewebsites.net/User/updateUser?userId=a9033a69-5916-49be-ba13-2269aaffa0ae&Name=Admin&PhoneNumber=8859984497 (Images are sent in Body )

Error Mg ---- Error writing MIME multipart body part to output stream."

Api Souce Code --

[Route("User/updateUser")]
public async Task<IHttpActionResult> updateUser(Guid userId, string Name, string PhoneNumber)
{
var uploadPath = "";
try
{
var userExist = db.Users.Where(w => w.Id == userId && w.IsActive == true).SingleOrDefault();
if (userExist != null)
{
var _uploadedDataId = userId;
var _exfilename = _uploadedDataId;

                var httpRequest = HttpContext.Current.Request;

                if (httpRequest.Files.Count != 0)
                {

                    uploadPath = System.Web.HttpContext.Current.Server.MapPath("~/storage/Uploads");
                    var multipartFormDataStreamProvider = new UploadMultipartFormProvider(uploadPath);

                    await Request.Content.ReadAsMultipartAsync(multipartFormDataStreamProvider);

                    string _localFileName = multipartFormDataStreamProvider
                        .FileData.Select(multiPartData => multiPartData.LocalFileName).FirstOrDefault();

                    string filePath = string.Empty;
                    string newfilePath = string.Empty;

                    filePath = uploadPath + '/' + Path.GetFileName(_localFileName);
                    string extension = Path.GetExtension(_localFileName);
                    string filename = Path.GetFileName(_localFileName);
                    newfilePath = uploadPath + '/' + _uploadedDataId + filename + extension;

                    File.Move(filePath, newfilePath);



                    userExist.Name = Name;
                    userExist.Phone = PhoneNumber;
                    userExist.ProfilePic = Common.Constants.FileUpload.FlateFilePath +"/storage"+"/Uploads/" + _uploadedDataId + filename + extension;
                    userExist.ModifiedDate = DateTime.Now;

                    db.SaveChanges();
                }
                else
                {
                    userExist.Name = Name;
                    userExist.Phone = PhoneNumber;
                    userExist.ModifiedDate = DateTime.Now;
                    db.SaveChanges();
                }


               // return Ok("User Details SuccessFully Updated");
                return Ok(uploadPath);
            }
            else
            {
                return Ok("User either Doesn't Exists or is currently inActive !!");
            }
        }
        catch (Exception ex)
        {

           // return BadRequest(ex.Message);
            return Ok(uploadPath);
        }
    }
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,679 questions
{count} votes