Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

image upload to webapi, sometime failed, help me

$
0
0

hi.
I develop first time use Xamarin Android.

this code well done sometime, but sometime it's failed with message.

Unhandled Exception.

System.Net.ProtocolViolationException: The number of bytes to be written is greater that the specified ContentLength.

Xamarin Android Code

using (var client = new HttpClient())
{
string strPath = tvImagePath.Text;
var content = new MultipartFormDataContent();
var imageContent = new StreamContent(new FileStream(strPath, FileMode.Open, FileAccess.Read));
content.Add(imageContent, "\"file\"", $"\"{strPath}\"");

            var uploadServiceBaseAddress = String.Format(Constants.RestUrl, "PhotoUpload", "uploadShopInfoPhoto");
            var httpResponseMessage = await client.PostAsync(uploadServiceBaseAddress, content);
            tvResult.Text = await httpResponseMessage.Content.ReadAsStringAsync();

}

WebAPI Code

try
{
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
int loop = 0;
foreach (string file in httpRequest.Files)
{
string strongFileNameTemp = DateTime.Now.ToString("yyMMddHHmmss");

                    var postedFile = httpRequest.Files[file];
                    var fileExt = postedFile.FileName.Split('\\').LastOrDefault().Split('.').LastOrDefault();
                    var newFileName = String.Format("{0}.{1}", strongFileNameTemp, fileExt);
                    var newFilePath = HttpContext.Current.Server.MapPath(String.Format("{0}/{1}", uploadFolder, newFileName));

                    postedFile.SaveAs(newFilePath);

                    val = String.Format("{0}/{1}", uploadFolder, newFileName);
                    loop++;
                    if (loop > 0) break;
                }
            }
        }
        catch (Exception exception)
        {
            val = exception.Message;

}

Help me please.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>