简体   繁体   中英

Saving file in IIS hosted WCF Service

Problem

When saving an image in the server using an absolute file name (eg. C:/tinkiwinki/dipsy.lala ) with the System.Drawing.Image.Save(Stream, ImageCodecInfo, EncoderParameters) method in a WCF Service hosted in IIS, it doesn't save the image and doesn't even throw an exception.

Background

We have a helper class and it has a SaveJpeg method:

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);
    }
}

We use it in a WCF OperationContract method like this:

string destDirectory = ServerDataFilePath.ImagesPath + @"\" + uploadID.ToString();
if (!Directory.Exists(destDirectory))
    Directory.CreateDirectory(destDirectory);
Tools.SaveJpeg(destDirectory + @"\" + fileName, img, 50);

The static ServerDataFilePath.ImagesPath is defined like this:

public class ServerDataFilePath
{
    public static string ImagesPath{ get { return ConfigurationManager.ConnectionStrings["ImagesPath"].ConnectionString; } }
}

Where it retrieves the ConnectionString from the Web.config file:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="ImagesPath" connectionString="C:\testServerImagesPath\"/>
  </connectionStrings>
</configuration>

Question

It works perfectly when we run it locally but when we deploy it in our test server using IIS, it doesn't save the images and doesn't throw any exceptions. Why was it like this?

Some Constraints

Code is in C#, framework 4, build in Visual Studio 2010 Professional. The service is deployed in Microsoft Windows Server 2003 R2 in IIS, service type is WCF in .NET 4.

Please help. Thanks in advance.

您的Web应用程序在IIS上运行的帐户很可能在C:\\tinkiwinki文件夹上没有写特权。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM