【发布时间】:2012-04-17 08:05:19
【问题描述】:
问题
当在 IIS 中托管的 WCF 服务中使用 绝对 文件名(例如 C:/tinkiwinki/dipsy.lala)和 System.Drawing.Image.Save(Stream, ImageCodecInfo, EncoderParameters) 方法将图像保存在服务器中时,它不会保存图像甚至不抛出异常。
背景
我们有一个辅助类,它有一个SaveJpeg 方法:
public class Tools
{
public static void SaveJpeg(string path, Image img, int quality)
{
if (quality < 0 || quality > 100)
throw new ArgumentOutOfRangeException("Quality must be between 0 and 100.");
EncoderParameter qualityParam =
new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
img.Save(path, jpegCodec, encoderParams);
}
}
我们在 WCF OperationContract 方法中使用它,如下所示:
string destDirectory = ServerDataFilePath.ImagesPath + @"\" + uploadID.ToString();
if (!Directory.Exists(destDirectory))
Directory.CreateDirectory(destDirectory);
Tools.SaveJpeg(destDirectory + @"\" + fileName, img, 50);
静态ServerDataFilePath.ImagesPath 是这样定义的:
public class ServerDataFilePath
{
public static string ImagesPath{ get { return ConfigurationManager.ConnectionStrings["ImagesPath"].ConnectionString; } }
}
它从 Web.config 文件中检索 ConnectionString 的位置:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<clear />
<add name="ImagesPath" connectionString="C:\testServerImagesPath\"/>
</connectionStrings>
</configuration>
问题
当我们在本地运行它时它工作得很好,但是当我们使用 IIS 将它部署到我们的测试服务器时,它不会保存图像并且不会抛出任何异常。为什么会这样?
一些限制
代码采用 C#,框架 4,在 Visual Studio 2010 Professional 中构建。该服务部署在 IIS 中的 Microsoft Windows Server 2003 R2 中,服务类型为 .NET 4 中的 WCF。
请帮忙。提前致谢。
【问题讨论】:
-
这张图片是从客户端上传的吗?如果是,您是否正确流式传输文件?
-
@BrijeshMishra 是的,我们实际上使用
byte[]来检索图像并将其转换为Image。我们已经在本地进行了测试,它运行良好,现在的问题是我们在 IIS 中部署服务时保存图像。