【问题标题】:Xamarin Forms Upload image to webpage as POSTXamarin Forms 将图像作为 POST 上传到网页
【发布时间】:2020-04-19 22:29:15
【问题描述】:

我有问题。我想将图像上传到网页,以便可以将其存储在我的服务器上。现在我可以找到很多将文件上传到服务器的示例,但是图片呢?

我已经尝试创建图像的字节数组并将其作为 POST 发送,但在以下行崩溃,说明 URI 太长:

var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("id", App.User.Id.ToString()));
postData.Add(new KeyValuePair<string, string>("image", ByteArray));
var content = new FormUrlEncodedContent(postData);

还有其他方法吗?

【问题讨论】:

    标签: c# xamarin xamarin.forms httpwebrequest httpclient


    【解决方案1】:

    你可以试试下面的方法:

       //In Xamarin
            public void UploadImage()
            {
                //create an instance of webclient
                System.Net.WebClient Client = new System.Net.WebClient();
    
                //add the header
                Client.Headers.Add("Content-Type", "binary/octet-streOpenWriteam");
    
                //uploads the file and gets the result in a byte[]
                // change `weburl/controller/actionmethod` to our weburl 
                byte[] result = Client.UploadFile("weburl/controller/actionmethod", "POST", "pathToFile");
    
                //converts the result[] to a string
                string resultString = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
            }
    

    更多详情可以查看:https://forums.xamarin.com/discussion/30653/how-to-upload-image-from-local-storage-to-asp-net-website

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-16
      • 2017-07-26
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      相关资源
      最近更新 更多