将图像从Android应用程序上传到ASP Web API

时间:2019-02-15 21:49:22

标签: c# android asp.net-mvc

我有从网络上传图像并发布人的webapi代码,我无法从我的Android应用程序上传图像,这是webapi代码:  [Route(“ AddCafeImage / {CafeId:int}”)]         公共HttpResponseMessage AddCafeImage(int CafeId)         {             字典dict = new Dictionary();             尝试             {

            var httpRequest = HttpContext.Current.Request;

            foreach (string file in httpRequest.Files)
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);

                var postedFile = httpRequest.Files[file];
                if (postedFile != null && postedFile.ContentLength > 0)
                {

                    int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB  

                    IList<string> AllowedFileExtensions = new List<string> { ".jpg", ".gif", ".png" };
                    var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                    var extension = ext.ToLower();
                    if (!AllowedFileExtensions.Contains(extension))
                    {

                        var message = string.Format("Please Upload image of type .jpg,.gif,.png.");
                        dict.Add("error", message);
                        return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
                    }
                    else if (postedFile.ContentLength > MaxContentLength)
                    {

                        var message = string.Format("Please Upload a file upto 1 mb.");

                        dict.Add("error", message);
                        return Request.CreateResponse(HttpStatusCode.BadRequest, dict);
                    }
                    else
                    {
                        var request = HttpContext.Current.Request;
                        string Url = request.Url.GetLeftPart(UriPartial.Authority) + request.ApplicationPath;
                        int id = CafetManager.SaveImage(CafeId, extension,Url);

                        var filePath = HttpContext.Current.Server.MapPath("~/ServiceProviderImages/" + id + extension);

                        postedFile.SaveAs(filePath);
                        var message1 = string.Format("Image Updated Successfully.");
                        return Request.CreateErrorResponse(HttpStatusCode.Created, message1); ;


                    }
                }


            }
            var res = string.Format("Please Upload a image.");
            dict.Add("error", res);
            return Request.CreateResponse(HttpStatusCode.NotFound, dict);
        }
        catch (Exception ex)
        {
            var res = string.Format(ex.ToString());
            dict.Add("error", res);
            return Request.CreateResponse(HttpStatusCode.NotFound, dict);
        }
    }

和android java代码是

公共无效uploadImage(文件文件){

    mSubscriptions.add(NetworkUtil.getRetrofit("ar","xxn4zZppNpORV8Kz7RWFizHBTJLiST2qrpu1pTrcjf4fnPOvGLaZkRBdBmvt"
            ,"application/x-www-form-urlencoded")
            .UploadImage("PXTmlji1upXSR0bnxiQv80hJKlk0X3lOQl972U7pB04C9kleL6Od13N23n8G",file)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(this :: handleResponse, this :: handleError));
}

我得到了messasge:“请上传图片”。 当我向请求添加图片时。 我想使用该webapi代码在android中上传图片。

0 个答案:

没有答案