在RESTful WCF服务中将图像作为附件上载

时间:2011-07-30 22:13:29

标签: c# wcf rest

我正在尝试将图像作为附件上传到REST WCF服务中,我收到以下错误。 “访问路径”C:\ ImageUpload“被拒绝” 我已为此文件夹启用了Full Contorl权限。我不明白为什么我收到这个错误。 我是WCF的新手,也是我从在线资源中收集的大部分代码。 感谢您是否可以告诉我如果我的代码中有任何错误。 这是我的代码。


REST WCF Service Code:

 [OperationContract]
 [WebInvoke(UriTemplate = "uploadImage/{parameter1}")]
  void uploadImage(Stream fileStream);

public void uploadImage(Stream fileStream)
        {
            string filePath = @"C:\ImageUpload";
            FileStream filetoUpload = new FileStream(filePath, FileMode.Create);

            byte[] byteArray = new byte[10000];
            int bytesRead, totalBytesRead = 0;

            do
            {
                bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
                totalBytesRead += bytesRead;
            }
            while (bytesRead > 0);
            filetoUpload.Write(byteArray, 0, byteArray.Length);
            filetoUpload.Close();
            filetoUpload.Dispose();
        }

这是我的测试客户端代码(简单.aspx网页)

 protected void btnUpload_Click(object sender, EventArgs e)
        {
            string file = FileUpload1.FileName;
            RESTService1Client client = new RESTService1Client();

            byte[] bytearray = null;
            string name = "";
            if (FileUpload1.HasFile)
            {
                name = FileUpload1.FileName;
                Stream stream = FileUpload1.FileContent;
                stream.Seek(0, SeekOrigin.Begin);
                bytearray = new byte[stream.Length];
                int count = 0;
                while (count < stream.Length)
                {
                    bytearray[count++] = Convert.ToByte(stream.ReadByte());
                }
            }  
            WebClient wclient = new WebClient();
            wclient.Headers.Add("Content-Type", "image/jpeg");
            client.uploadImage(FileUpload1.FileContent);
}

2 个答案:

答案 0 :(得分:0)

这可能与WCF或您的代码无关。对于IIS进程用户来说,该文件夹的权限很可能不足。默认情况下,ASP.NET用户是网络服务。

尝试仅为ASP.NET应用程序创建新的Windows用户。授予该用户对上传文件夹的显式读/写访问权限。然后使用Impersonation使ASP.NET使用该用户。 http://www.codeproject.com/Articles/107940/Back-to-Basic-ASP-NET-Runtime-Impersonation

答案 1 :(得分:0)

重写服务器端:

REST WCF服务代码:

[OperationContract]
[WebInvoke(UriTemplate = "uploadImage/{parameter1}/{parameter2}")]
void uploadImage(Stream fileStream, string fileName);

public void uploadImage(Stream fileStream, string fileName)
    {
        string filePath = @"C:\ImageUpload\";
        using (FileStream filetoUpload = new FileStream(filePath + fileName, FileMode.Create))
        {
            byte[] byteArray = new byte[10000];
            int bytesRead = 0;

            do
            {
                bytesRead = fileStream.Read(byteArray, 0, byteArray.Length);
                if (bytesRead > 0)
                {
                    filetoUpload.Write(byteArray, 0, bytesRead);
                }
            }

            while (bytesRead > 0);
        }
    }

和您的客户方面:

 protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            RESTService1Client client = new RESTService1Client();

            client.uploadImage(FileUpload1.FileContent, Path.GetFileName(FileUpload1.FileName));
        }  
}