0

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)
        {
            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);
}
Henry
  • 847
  • 6
  • 24
  • 53
  • You gave Full Control access to which user? Are you sure the WCF service is running under that account? – John Saunders Jul 30 '11 at 22:18
  • BOth WCF Service and client are located on my local system. I gave full access to all the users I found under the security tab of the "ImageUpload" folder. – Henry Jul 30 '11 at 22:28
  • Try giving access to "Everyone"` – John Saunders Jul 30 '11 at 22:29
  • I tried giving access to "Everyone". Still not working. – Henry Jul 30 '11 at 22:35
  • This question has a lot of good information which can at least help you utilize the appropriate tools to illuminate what's happening at execution time: http://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permissions – Jesse C. Slicer Jul 31 '11 at 03:24
  • Um, ACCESS DENIED is because you're trying to open a `FileStream` for write on a directory name, rather than a file. – Jesse C. Slicer Jul 31 '11 at 03:30
  • And, you'll only be writing the LAST 10,000 bytes of the stream to whatever file you save to as you're discarding everything before it in repeated reads. – Jesse C. Slicer Jul 31 '11 at 03:31

2 Answers2

0

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

Ash Eldritch
  • 1,504
  • 10
  • 13
0

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));
        }  
}
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • Note, I haven't tested this at all, but there were some glaring bugs in the original code that, once removed, might give you a leg up. – Jesse C. Slicer Jul 31 '11 at 04:04
  • HI Jesse, Thanks for your time for writing the code for me. I tried your code and I am getting the following error. "For request in operation uploadImage to be a stream the operation must have a single parameter whose type is Stream". You have any idea whats causing this error. ONce againg thanks for your time. – Henry Jul 31 '11 at 04:26
  • Well, the error says exactly what's wrong - can't have two parameters (the fileName parameter I added). Might want to try to get the filename from client to server (or generate on the server) in a different manner. – Jesse C. Slicer Jul 31 '11 at 15:54