Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am using Web service to upload file from silverlight client. I managed to upload files successfully using below code[webservice] from same computer but i get error on uploading from network.

I tried fixes like setting file permissions to full, but none worked.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Web.Hosting;
using System.Security;

namespace SilverlightApplication5.Web
{
    [WebService(Namespace = "http://***.com/testfile/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class FileUploadService : System.Web.Services.WebService
    {
        [WebMethod]
        public string TestService()
        {
            return "Service test suceed";
        }

        [WebMethod]
        public string Upload(string id, string mode, string path, string name, string filedata, bool overwrite, string tag, bool final)
        {
            try
            {
                string filename = string.Empty;

                if (Directory.Exists(@HostingEnvironment.ApplicationPhysicalPath + "/Documents/" + path) == false)
                {
                    Directory.CreateDirectory(@HostingEnvironment.ApplicationPhysicalPath + "/Documents/" + path);
                }
                try
                {
                    filename = Server.MapPath("~") + @"\Documents\" + path.Replace("/", @"\") + @"\" + name;

                    if (mode == "new")
                    {
                        if (File.Exists(filename) == true)
                        {
                            if (overwrite)
                            {
                                File.Delete(filename);
                            }
                            else
                            {
                                return "File Already Exists";
                            }
                        }

                        WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Create);
                    }
                    else
                    {
                        WriteFile(filename, Convert.FromBase64String(filedata), FileMode.Append);
                    }
                }
                catch (Exception ex)
                {
                    File.Delete(filename);
                    return "File Write Error: " + ex.Message;
                }

                return "ok";
            }
            catch (SecurityException ex)
            {
                return ex.Message.ToString();
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        private void WriteFile(string filename, byte[] content, FileMode fileMode)
        {
            Stream target = null;
            BinaryWriter writer = null;

            try
            {
                target = File.Open(filename, fileMode);
                writer = new BinaryWriter(target);

                writer.Write(content);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not write to file: " + filename + " - " + ex.Message);
            }
            finally
            {
                if (target != null)
                {
                    target.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }
    }
}


please tell me where i am doing wrong in hosting this service.
thanks in advance.
Posted
Updated 19-Jul-15 23:30pm
v3
Comments
Herman<T>.Instance 16-Jul-15 8:29am    
what does the IIS server says in it loggings?
[no name] 18-Jul-15 13:28pm    
what kind on Exception you have? What the error message says?

1 solution

Before checking the code, make sure that saving files is ENABLED in client browser settings. If it's not, you won't be able to write to the disk and save files.

Secondly, in the Silverlight application you should write to the Application Data storage only:

C#
if (Application.Current.HasElevatedPermissions)
{
    string perUserApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}


You can check this out to be more familiar with trusted applications permissions:
https://msdn.microsoft.com/en-us/library/ee721082(v=vs.95).aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900