图片发布到Asp.Net Web API

时间:2018-08-17 09:01:26

标签: c# asp.net wpf asp.net-web-api

我正在处理WPF应用程序。我要发布从OpenFileDialog中选择的图像。而且我想返回网址

URL应该是这样的: http://localhost:52185/api/Images/123456.jpg

但是我的网址: http://localhost:52185/api/Images/DD8609E3-58A0-E811-B814-D050994A8C7D

如何将图像保存到本地主机?

发布方法:

[HttpPost]
public IHttpActionResult Post(ImageDTO request)
{
    var buffer = Convert.FromBase64String(request.Image);
    HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(buffer);

    using( var content = new MultipartFormDataContent())
    {
        byte[] bytes = new byte[objFile.InputStream.Length + 1];
        objFile.InputStream.Read(bytes, 0, bytes.Length);
        var fileContent = new ByteArrayContent(bytes);
        fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = request.Name };
        content.Add(fileContent);
        objFile.SaveAs("http://localhost:52185/api/Images/1.jpg");
    }
    return Ok("http://imageurl");
}

客户:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog fileDialog = new OpenFileDialog();
    fileDialog.Multiselect = false;
    fileDialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
    fileDialog.ShowDialog();

    var img = Convert.ToBase64String(File.ReadAllBytes(fileDialog.FileName));

    HttpClient cli = new HttpClient();
    var url = "http://localhost:52185/api/Images";
    var parameters = new Dictionary<string, string> { { "image", img } };
    var encodedContent = new FormUrlEncodedContent(parameters);

    var response = await  cli.PostAsync(url, encodedContent).ConfigureAwait(false);
    if (response.StatusCode == HttpStatusCode.OK)
    {
        MessageBox.Show(response.ToString());
    }
}

2 个答案:

答案 0 :(得分:0)

正如您没有提到在哪个阶段遇到困难,我认为您已经设法正确读取输入流。您可能仍需要修改此代码以适合您的编码样式。

string f = @"C:\yourfolder\yourfile.png"; // @"c:\yourAPIRoot\Images\1.jpg
using (var binaryReader = new System.IO.BinaryReader(objFile.InputStream))
{
    byte[] fileData = binaryReader.ReadBytes(objFile.ContentLength);
    using (System.IO.FileStream FileStream1 =
         new System.IO.FileStream(f, System.IO.FileMode.Create,
                                  System.IO.FileAccess.Write))
    {
        FileStream1.Write(fileData, 0, fileData.Length);
        FileStream1.Close();
    }
}  
//return "http://localhost:52185/api/images/1.jpg"
return "http://yourdomain.com/yourfolder/yourfile.png"; //modify this to your return type

答案 1 :(得分:0)

我认为您不能在SaveAs方法中使用虚拟路径,而且代码中bytes/fileContent的目的是什么?加/api/是非物理地址,我不确定这是否行得通,无论如何,您可以将方法更改为类似的方式,

[HttpPost]
public IHttpActionResult Post(ImageDTO request)
{
    var buffer = Convert.FromBase64String(request.Image);
    HttpPostedFileBase objFile = (HttpPostedFileBase)new MemoryPostedFile(buffer);

    // full virtual path of the image name
    // TODO: right now, request.Name is assumed to be the image file name
    var virtualPath = string.Format("/api/Images/{0}.jpg", request.Name);

    using( var content = new MultipartFormDataContent())
    {
        // **** I am NOT sure what is the purpose of bytes/fileContent here ****
        byte[] bytes = new byte[objFile.InputStream.Length + 1];
        objFile.InputStream.Read(bytes, 0, bytes.Length);
        var fileContent = new ByteArrayContent(bytes);
        fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = request.Name };
        content.Add(fileContent);
        // here we are converting the virtual path to physical path
        var physicalPath = Server.MapPath(virtualPath);
        objFile.SaveAs(physicalPath);
    }
    // I guess here you want to return the newly created path for the image
    return Ok(virtualPath);
}