【问题标题】:Uploading image as attachment in RESTful WCF Service在 RESTful WCF 服务中将图像作为附件上传
【发布时间】:2011-07-30 22:13:28
【问题描述】:

我正在尝试将图像作为附件上传到 REST WCF 服务中,但出现以下错误。 “访问路径“C:\ImageUpload”被拒绝” 我已启用对此文件夹的完全控制权限。我不明白为什么会收到此错误。 我是 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);
}

【问题讨论】:

  • 您向哪个用户授予了完全控制权限?您确定 WCF 服务正在该帐户下运行吗?
  • WCF 服务和客户端都位于我的本地系统上。我授予了我在“ImageUpload”文件夹的安全选项卡下找到的所有用户的完全访问权限。
  • 尝试授予“所有人”的访问权限`
  • 我尝试授予“所有人”的访问权限。还是不行。
  • 关于这个家伙的任何更新。谢谢

标签: c# wcf rest


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这样重写服务器端:

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

    【讨论】:

    • 请注意,我根本没有对此进行测试,但原始代码中存在一些明显的错误,一旦删除,可能会给您带来帮助。
    • 您好 Jesse,感谢您抽出宝贵时间为我编写代码。我尝试了您的代码,但出现以下错误。 “要使操作中的请求 uploadImage 成为流,该操作必须有一个类型为 Stream 的参数”。你知道是什么导致了这个错误。再次感谢您的宝贵时间。
    • 嗯,错误说明了问题所在 - 不能有两个参数(我添加的 fileName 参数)。可能想尝试以不同的方式从客户端获取文件名到服务器(或在服务器上生成)。
    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 2011-10-10
    • 2012-02-05
    • 2011-08-07
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多