C# 将文件从获取的路径复制到域
您好,如果可以将文件从获取的路径复制到域中,我尝试了以下操作,但遇到异常,因为不支持C# 将文件从获取的路径复制到域,c#,C#,您好,如果可以将文件从获取的路径复制到域中,我尝试了以下操作,但遇到异常,因为不支持uri格式。。有人能帮我复制文件吗 string filePath = "D:\\Folder\\filename.jpg"; FileInfo fileInfo = new FileInfo(filePath); if (fileInfo.Exists) { path = "http://WWW.somedomain.com"; string temppath = path + "/Te
uri格式。。有人能帮我复制文件吗
string filePath = "D:\\Folder\\filename.jpg";
FileInfo fileInfo = new FileInfo(filePath);
if (fileInfo.Exists)
{
path = "http://WWW.somedomain.com";
string temppath = path + "/Temp" + "/" + fileInfo.Name;
if (!File.Exists(temppath))
{
var uri = new Uri(temppath);
File.Copy(filePath, uri.AbsoluteUri);
}
您要检查服务器上是否存在文件。这不可能使用File.Exist
方法,因为它不支持URI。此方法期望相对路径并检查机器上是否存在(物理位置)
在这种情况下,您应该使用WebRequest
并从服务器获取响应。如果服务器返回404,那么您的文件在服务器上不存在,或者您可以检查内容长度
WebRequest request = WebRequest.Create(new Uri(temppath));
request.Method = "HEAD";
WebResponse response = request.GetResponse()
var contentLength = response.ContentLength;
if (contentLength < 0)
{
// file doesn't exist.
}
WebRequest-request=WebRequest.Create(新Uri(temppath));
request.Method=“HEAD”;
WebResponse=request.GetResponse()
var contentLength=response.contentLength;
if(contentLength<0)
{
//文件不存在。
}
Hi Pawan谢谢,但我的要求是我正在从javascript调用一个服务,所以这不起作用。我如何按照您所说的检查文件,您能给我一个sample@demouser您可以在uri部分发送文件的完整路径。例如,本例中的temppath
。请参阅更新。嗨,sachin如果不存在,我如何复制?@demouser您不能使用file.copy
方法将文件上载到远程服务器。您的服务器应该向您公开一个web服务,您可以通过该服务将文件上载到服务器上。