asp.net:GDI +中发生了一般错误

Mah*_*iya 2 asp.net base64 web-services image-upload

我创建了一个asp.net 4.0 Web应用程序,它有一个用于上传图像的Web服务.我通过从我的移动应用程序向Web服务发送Base64字符串形式的图像来上传图像.

以下是我的代码:

public string Authenticate(string username, string password, string fileID, string imageData)
    {
        Dictionary<string, string> responseDictionary = new Dictionary<string, string>();
        bool isAuthenticated = true; // Set this value based on the authentication logic

        try
        {
            if (isAuthenticated)
            {
                UploadImage(imageData);

                string result = "success";
                var message = "Login successful";

                responseDictionary["status"] = result;
                responseDictionary["message"] = message;
            }
        }
        catch (Exception ex)
        {

            responseDictionary["status"] = ex.Message;
            responseDictionary["message"] = ex.StackTrace;
        }

        return new JavaScriptSerializer().Serialize(responseDictionary);
    }

    private void UploadImage(string uploadedImage)
    {
        // Convert Base64 String to byte[]
        byte[] imageBytes = Convert.FromBase64String(uploadedImage);
        MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

        System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Image.FromStream(ms);

        string uploadPath = Server.MapPath("..\\uploads\\") + DateTime.Now.Ticks.ToString() + ".jpeg";
        ms.Close();

        bitmap.Save(uploadPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        bitmap.Dispose();
    }    
Run Code Online (Sandbox Code Playgroud)

这段代码在我的本地ASP.NET开发服务器上工作正常,我能够在"uploads"目录中看到上传的图像.但是,在将代码传输到FTP目录后,我现在收到以下错误:

A generic error occurred in GDI+

我通过创建一个虚拟的.aspx页面并在page_load上创建一个文本文件来检查上传目录是否具有适当的权限,并且它工作正常.

即使在谷歌搜索后,我也无法解决这个问题.任何人都可以帮我解决这个问题吗?

非常感谢提前.

Wik*_*hla 6

而不是直接写入文件,将位图保存到a MemoryStream,然后将流的内容保存到磁盘.这是一个古老的,已知的问题,坦率地说,我不记得为什么会这样.

 MemoryStream mOutput = new MemoryStream();
 bmp.Save( mOutput, ImageFormat.Png );
 byte[] array = mOutput.ToArray();

 // do whatever you want with the byte[]
Run Code Online (Sandbox Code Playgroud)

在你的情况下,它可能是

private void UploadImage(string uploadedImage)
{
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(uploadedImage);

    string uploadPath = Server.MapPath("..\\uploads\\") + DateTime.Now.Ticks.ToString() + ".jpeg";

    // store the byte[] directly, without converting to Bitmap first 
    using ( FileStream fs = File.Create( uploadPath ) )
    using ( BinaryWriter bw = new BinaryWriter( fs ) )
       bw.Write( imageBytes );
}    
Run Code Online (Sandbox Code Playgroud)

要么

private void UploadImage(string uploadedImage)
{
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(uploadedImage);
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

    System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)Image.FromStream(ms);

    string uploadPath = Server.MapPath("..\\uploads\\") + DateTime.Now.Ticks.ToString() + ".jpeg";
    ms.Close();

    // convert to image first and store it to disk
    using ( MemoryStream mOutput = new MemoryStream() )
    {  
        bitmap.Save( mOutput, System.Drawing.Imaging.ImageFormat.Jpeg);
        using ( FileStream fs = File.Create( uploadPath ) )
        using ( BinaryWriter bw = new BinaryWriter( fs ) )
            bw.Write( mOutput.ToArray() );
    }
}    
Run Code Online (Sandbox Code Playgroud)