如何使用 FileStream 将 zip 文件复制到远程服务器位置?
How to copy zip file to remote server location using FileStream?
我正在尝试使用文件流将 zip 文件复制到远程服务器位置,下面是我的网络方法:
[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));
}
但我不知道如何将 byteData 作为 zip 写入目标。
之前我使用的是 File.Copy
方法,但它不适用于远程服务器。
您可以使用 BinaryWriter。请参阅此代码:
首先添加这个命名空间:使用 System.IO
var bytes = File.ReadAllBytes("YOUR_SOURCE_PATH");
BinaryWriter writer = new BinaryWriter(File.OpenWrite("DESTINATION_PATH.zip"));
writer.Write(bytes);
考虑一下,当您将文件数据读取为字节数组时,无论您的文件扩展名如何。
using (var outStream = new FileStream(somePath, FileMode.Write))
{
using (var inStream = new FileStream(localPath, FileMode.Read))
{
inStream.CopyTo(outStream);
}
}
System.IO.File.Copy可以,但前提是你的电脑有访问目的地的权限
最方便的验证方法之一是找到网络共享驱动器,然后将文件复制到那里。
即
File.Copy(@"c:\temp\MyFile.txt", @"\server\folder\Myfile.txt", true);
在很多博客中搜索后,我发现我们需要某种与客户端机器交互的客户端控件。所以我使用共享路径将该文件上传到目的地,而不是我的情况下的一些随机路径 "c:\temp\abc.zip".
下面是我用来完成该任务的 WebMethod。
[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'}";
}
}
我正在尝试使用文件流将 zip 文件复制到远程服务器位置,下面是我的网络方法:
[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));
}
但我不知道如何将 byteData 作为 zip 写入目标。
之前我使用的是 File.Copy
方法,但它不适用于远程服务器。
您可以使用 BinaryWriter。请参阅此代码:
首先添加这个命名空间:使用 System.IO
var bytes = File.ReadAllBytes("YOUR_SOURCE_PATH");
BinaryWriter writer = new BinaryWriter(File.OpenWrite("DESTINATION_PATH.zip"));
writer.Write(bytes);
考虑一下,当您将文件数据读取为字节数组时,无论您的文件扩展名如何。
using (var outStream = new FileStream(somePath, FileMode.Write))
{
using (var inStream = new FileStream(localPath, FileMode.Read))
{
inStream.CopyTo(outStream);
}
}
System.IO.File.Copy可以,但前提是你的电脑有访问目的地的权限
最方便的验证方法之一是找到网络共享驱动器,然后将文件复制到那里。 即
File.Copy(@"c:\temp\MyFile.txt", @"\server\folder\Myfile.txt", true);
在很多博客中搜索后,我发现我们需要某种与客户端机器交互的客户端控件。所以我使用共享路径将该文件上传到目的地,而不是我的情况下的一些随机路径 "c:\temp\abc.zip".
下面是我用来完成该任务的 WebMethod。
[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'}";
}
}