Naveen Kumar
Naveen Kumar

Reputation: 1432

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.

Upvotes: 0

Views: 1281

Answers (4)

Naveen Kumar
Naveen Kumar

Reputation: 1432

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'}";
    }
}

Upvotes: 0

CHRISQQ
CHRISQQ

Reputation: 145

System.IO.File.Copy is ok, but the prerequisite is that your pc has right to access to the destination.

One of the most convenient way to verify is finding a network shared drive and then copy your file to there. i.e.

File.Copy(@"c:\temp\MyFile.txt", @"\\server\folder\Myfile.txt", true);

Upvotes: 1

bommelding
bommelding

Reputation: 3037

using (var outStream = new FileStream(somePath, FileMode.Write))
{
   using (var inStream = new FileStream(localPath, FileMode.Read))
   {
        inStream.CopyTo(outStream);
   }
}

Upvotes: 2

Saeed Bolhasani
Saeed Bolhasani

Reputation: 580

you can use BinaryWriter. See this code:
first add this namespace: using System.IO

   var bytes = File.ReadAllBytes("YOUR_SOURCE_PATH");

   BinaryWriter writer = new BinaryWriter(File.OpenWrite("DESTINATION_PATH.zip"));

   writer.Write(bytes);

Consider when you read file data as byte array, there is no matter your file extension.

Upvotes: 0

Related Questions