在 RESTful WCF 服务中将图像作为附件上传 [英] Uploading image as attachment in RESTful WCF Service

查看:53
本文介绍了在 RESTful WCF 服务中将图像作为附件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像作为附件上传到 REST WCF 服务中,但出现以下错误.访问路径C:\ImageUpload"被拒绝"我已启用对此文件夹的完全控制权限.我不明白为什么我会收到这个错误.我是 WCF 的新手,也是我从在线资源中收集的大部分代码.感谢您让我知道我的代码是否有任何错误.这是我的代码.

I am trying to upload an image as an attachment in REST WCF Service and I am getting the following error. "Access to path "C:\ImageUpload" is denied" I have enabled Full Contorl permissions to this folder. I dont understand why I am getting this error. I am new to WCF, and the most of the code I gathered from online resources. Appreciate if you could let me know If there is any mistake in my code. Here is my code.

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 网页)


This is my Test Client Code(Simple .aspx web page)

 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 或您的代码无关.对于 IIS 进程用户来说,该文件夹的权限确实很可能不足.默认情况下,ASP.NET 用户是网络服务.

It's likely nothing to do with WCF or your code. It really is highly probable that permissions on that folder are insufficient for the IIS process user. By default the ASP.NET user is Network Service.

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

Try creating a new Windows user just for your ASP.NET application. Grant that user explicit read/write access to the upload folder. Then use Impersonation to make ASP.NET use that user. http://www.codeproject.com/Articles/107940/Back-to-Basic-ASP-NET-Runtime-Impersonation

这篇关于在 RESTful WCF 服务中将图像作为附件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆