Search code examples
c#asp.netweb-servicesfilestream

How to copy zip file to remote server location using FileStream?


I'm trying to copy zip file to remote server location using file stream, below is my web method:

[WebMethod]
public void SavePackage(string args = "{}")
{
    FileStream fs = new FileStream(@"c:\temp\abc.zip", FileMode.Open, FileAccess.Read);
    byte[] byteData = new byte[fs.Length];
    fs.Read(byteData, 0, System.Convert.ToInt32(fs.Length));
}

but I don't know how to write byteData to destination as a zip.

before I was using the File.Copy method but that is not working for remote server.


Solution

  • After searching in many blogs, I have found we need some kind of client control which interacts with client machine. So I have used the shared path to upload that file to destination rather than some random path in my case "c:\temp\abc.zip".

    Below is my WebMethod to do that task.

    [WebMethod]
    public string SavePackage(string args = "{}")
    {
        try
        {
            // here i am accepting json args as parameter
            string sourcePath = string.Empty, type = string.Empty, category = string.Empty, description = string.Empty, additionalComments = string.Empty;
            var jsonargs = (JObject)JsonConvert.DeserializeObject(args);
    
            if (jsonargs.Count == 0)
            {
                return "{'message':'No parameters', 'status':'404'}";
            }
    
            foreach (var item in jsonargs)
            {
                sourcePath = (item.Key.ToLower() != "sourcepath" || !string.IsNullOrEmpty(sourcePath)) ? sourcePath : item.Value.ToString().Replace(@"""", "").Replace(@"\\", @"\"); // shared path
                type = (item.Key.ToLower() != "type" || !string.IsNullOrEmpty(type)) ? type : item.Value.ToString().Replace(@"""", "");
                category = (item.Key.ToLower() != "category" || !string.IsNullOrEmpty(category)) ? category : item.Value.ToString().Replace(@"""", "");
                description = (item.Key.ToLower() != "description" || !string.IsNullOrEmpty(description)) ? description : item.Value.ToString().Replace(@"""", "");
                additionalComments = (item.Key.ToLower() != "additionalcomments" || !string.IsNullOrEmpty(additionalComments)) ? additionalComments : item.Value.ToString().Replace(@"""", "");
            }
    
            if (!Path.GetExtension(sourcePath).Equals(".zip"))
            {
                return "{'message':'File source path is not in a zip format', 'status':'404'}";
            }
    
            var filename = sourcePath.Remove(0, sourcePath.LastIndexOf("\\", StringComparison.Ordinal) + 1);    
            var tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
            Directory.CreateDirectory(tempDir);
            var destPath = Path.Combine(tempDir, filename);
            File.Copy(sourcePath, destPath, true);
    
            if (!File.Exists(destPath))
            {
                return "{'message':'File not copied', 'status':'404'}";
            }
    
            return "{'message':'OK', 'status':'200'}";
        }
        catch (Exception ex)
        {
            return "{'message':'error', 'status':'404'}";
        }
    }