开发者

Uploading image as attachment in RESTful WCF Service

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

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

 protected void btnUpload_Click(object sender, EventArgs e)
     开发者_JAVA技巧   {
            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);
}


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.

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


Rewrite server side as such:

REST WCF Service Code:

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

and your client side as such:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜