2

我正在尝试使用 web api 将图像从我的 Windows 桌面应用程序 (VB.NET) 上传到 Web 服务器

代码在本地机器上正确运行。但在 Web 服务器上运行时失败,并显示错误消息 A generic error occurred in GDI+。

以下是接受图像的 WebApi 代码

公共无效 PostFile(ImageData objImage)

    {
        Image img = BytesToImage(objImage.ImageFile);
        string ImageName = objImage.EmployeeGUID.ToString() + ".Jpg";
        string FilePath = "";
        FilePath = System.Web.HttpContext.Current.Server.MapPath("~/photo") ;

        try { 
    img.Save(FilePath + '\\' + ImageName.ToString(), System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (Exception ex) {
        }
    }


  public class ImageData
  {
        public long EmployeeCode;
        public Guid EmployeeGUID; 
        public byte[] ImageFile ;
    }

    private Image BytesToImage(byte[] ImageBytes)
   {
        Image imgNew;
        MemoryStream memImage = new MemoryStream(ImageBytes);
        imgNew = Image.FromStream(memImage);
        return imgNew;
    }

以下代码是从中
上传图像的 VB.NET 代码(Windows 窗体应用程序)

公共子发送文件()

Dim EmployeeGUID As GUID
Dim EmployeeCode As long
    Dim ImagefileToSend As String
    Dim objImage As ImageData
    Dim client As New HttpClient


    client.BaseAddress = New Uri(WebApiPath)
    client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

    objImage = New ImageData()
    objImage.EmployeeCode = EmployeeCode
    objImage.EmployeeGUID = EmployeeGUID
    objImage.ImageFile = ImageToBytes(Image.FromFile(ImagefileToSend))

    Dim jsonFormatter As MediaTypeFormatter = New JsonMediaTypeFormatter()
    Dim content As HttpContent = New ObjectContent(GetType(ImageData), objImage, jsonFormatter)
    Dim result As System.Net.Http.HttpResponseMessage

    Try
      result = client.PostAsync("api/GetFile", content).Result
    Catch ex As Exception
    End Try
End Sub

Private Class ImageData
    Public EmployeeCode As Long
    Public EmployeeGUID As Guid
    Public ImageFile As Byte()
End Class

Private Function ImageToBytes(ByVal image As Image) As Byte()
    Dim memImage As New IO.MemoryStream
    Dim bytImage() As Byte

    image.Save(memImage, image.RawFormat)
    bytImage = memImage.GetBuffer()

    Return bytImage
End Function
4

1 回答 1

2

照片目录没有写权限。现在它工作正常

于 2013-04-25T14:21:37.627 回答