C# 如何使用FileStream将zip文件复制到远程服务器位置?
我正在尝试使用文件流将zip文件复制到远程服务器位置,下面是我的web方法:C# 如何使用FileStream将zip文件复制到远程服务器位置?,c#,asp.net,web-services,filestream,C#,Asp.net,Web Services,Filestream,我正在尝试使用文件流将zip文件复制到远程服务器位置,下面是我的web方法: [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,
[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);
考虑一下,当您将文件数据读取为字节数组时,无论您的文件扩展名是什么 您可以使用BinaryWriter。请参阅此代码:
using (var outStream = new FileStream(somePath, FileMode.Write))
{
using (var inStream = new FileStream(localPath, FileMode.Read))
{
inStream.CopyTo(outStream);
}
}
首先添加这个名称空间:使用System.IO
var bytes = File.ReadAllBytes("YOUR_SOURCE_PATH");
BinaryWriter writer = new BinaryWriter(File.OpenWrite("DESTINATION_PATH.zip"));
writer.Write(bytes);
考虑一下,当您将文件数据读取为字节数组时,无论您的文件扩展名是什么 System.IO.File.Copy可以,但前提是您的电脑有权访问目标
using (var outStream = new FileStream(somePath, FileMode.Write))
{
using (var inStream = new FileStream(localPath, FileMode.Read))
{
inStream.CopyTo(outStream);
}
}
最方便的验证方法之一是找到网络共享驱动器,然后将文件复制到该驱动器。
i、 e
System.IO.File.Copy可以,但前提是您的电脑有权访问目标 最方便的验证方法之一是找到网络共享驱动器,然后将文件复制到该驱动器。 i、 e
在搜索了很多博客后,我发现我们需要某种与客户机交互的客户机控件。因此,我使用共享路径将该文件上传到目的地,而不是在我的示例“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'}";
}
}
在搜索了很多博客后,我发现我们需要某种与客户机交互的客户机控件。因此,我使用共享路径将该文件上传到目的地,而不是在我的示例“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'}";
}
}
您可以将
byte[]
直接写入流,BW不会添加任何内容。对于大文件,您应该使用while循环将其分解为块。@saeedvar docPath=sourcePath;var filestream=newfilestream(docPath,FileMode.Open,FileAccess.Read);var len=(int)filestream.Length;var文件=新字节[len];读取(文档,0,len);var destdocPath=destPath;var objfilestream=newfilestream(destdocPath,FileMode.Create,FileAccess.ReadWrite);objfilestream.Write(document,0,document.Length);上面的代码给了我一个错误:找不到文件“C:\Temp\abc.zip”。
我正在使用主机上的邮递员发布请求。@Naveen Kumar您确定您的文件存在吗?@SaeedBolhasani是的,它存在于我的客户机上,并为WebMethod提供相同的客户机路径。您可以将字节[]
直接写入流,BW不添加任何内容。对于一个大文件,您应该使用while循环将其分解成块。@saeedvar docPath=sourcePath;var filestream=newfilestream(docPath,FileMode.Open,FileAccess.Read);var len=(int)filestream.Length;var文件=新字节[len];读取(文档,0,len);var destdocPath=destPath;var objfilestream=newfilestream(destdocPath,FileMode.Create,FileAccess.ReadWrite);objfilestream.Write(document,0,document.Length);上面的代码给了我一个错误:找不到文件“C:\Temp\abc.zip”。
我正在使用主机上的邮递员发布请求。@Naveen Kumar您确定您的文件存在吗?@SaeedBolhasani是的,它存在于我的客户机上,并为WebMethod提供相同的客户机路径。您能在该服务器上打开输出流吗?如果您的文件大小(如果较大)。fs.length将出现内存不足错误。尝试使用缓冲区您是否能够在该服务器上打开输出流?如果您的文件大小很大。fs.length将出现内存不足错误。尝试使用缓冲区如果您有管理员凭据和任何共享文件夹,您可以使用@“\\server\c$\somefolder\anotherfolder\myfile.txt”
将其复制到c:\somefolder\anotherfolder\myfile.txt,而无需共享文件夹。您好,Cid,您说得对。但我想知道,如果没有权限,FileStream或任何使用System.IO的方法是否可以将文件写入远程服务器?@NKLI我没有共享路径,因此file.Copy无法工作。如果您有管理员凭据和任何共享文件夹,您可以使用“\\server\c$\somefolder\anotherfolder\myfile.txt”
将其复制到c:\somefolder\anotherfolder\myfile.txt
而无需共享文件夹。您好,Cid,您好。但我想知道,如果没有权限,FileStream或任何使用System.IO的方法是否可以将文件写入远程服务器?@NKLI我没有共享路径,因此file.Copy无法工作。在保存包方法中没有声明日志记录对象,您在整个代码中都使用了该方法。我甚至怀疑这段代码是否可以编译。在SavePackage
方法中没有声明logging
对象,而您在整个代码中都使用了该方法。我甚至怀疑这段代码是否能够编译。