将图像上传到iis服务器并在客户端上显示

时间:2014-09-05 09:15:21

标签: windows-runtime asp.net-web-api iis-express

我在数据库中有表PRODUCT(Id,Name,ImagePath)。 在WINRT客户端,用户可以在其文件夹中插入带有图像的产品,并通过POST方法发送。

try
        {
            HttpClient client = new HttpClient();

            var reader = new DataReader(stream.GetInputStreamAt(0));
            var bytes = new byte[stream.Size];
            await reader.LoadAsync((uint)stream.Size);
            reader.ReadBytes(bytes);

            HttpContent httpContent = new ByteArrayContent(bytes);

            var response = await client.PostAsync("http://192.168.51.115:9696/api/MediaUpload/UploadMediaFile?fileName=}" + fileName, httpContent);
            HttpResponseMessage message = response;

            if (message.IsSuccessStatusCode)
            {
                Status.Text = "Upload completed";
            }
            else
            {
                Status.Text = message.StatusCode.ToString();
            }
        }
        catch (Exception ex)
        {
            Status.Text = ex.Message + " Error";
        }

在服务器中将收到图像字节数组并存储在本地文件夹中。

[HttpPost]
    [ResponseType(typeof(string))]
    public async Task<IHttpActionResult> UploadMediaFile(string fileName)
    {
        try
        {
            byte[] bytes = await Request.Content.ReadAsByteArrayAsync();

            string path = ??? // What is path???

            MemoryStream ms = new MemoryStream(bytes);

            FileStream fs = new FileStream(path, FileMode.Create);

            ms.WriteTo(fs);

            ms.Close();
            fs.Close();
            fs.Dispose();

            return Ok(path);
        }
        catch(Exception ex)
        {
            throw;
        }
    }

然后获取在客户端上显示图像的路径,如

<Image Height="300"
       Width="300"
       Source="http://192.168.51.115:9696/FileUpload/image.png"/>

0 个答案:

没有答案