上传文件到文件服务器 [英] Uploading files to file server

查看:177
本文介绍了上传文件到文件服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的链接,我写了一个代码为我的应用程序。我没有能够得到它,虽然正确,请参阅该链接,帮我一下吧......加时赛



http://stackoverflow.com/questions/263518/c-uploading-files-to-file-server






下面是我的代码: -

 保护无效的button1_Click(对象发件人,EventArgs五)
{

=文件路径FileUpload1.FileName;

{
WebClient的客户端=新的WebClient();

的NetworkCredential NC =新的NetworkCredential(UNAME,密码);

乌里阿迪=新的URI(\\\\192.168.1.3\\upload\\);
client.Credentials = NC;
字节[] = arrReturn client.UploadFile(阿迪,文件路径);

Console.WriteLine(arrReturn.ToString());
}
赶上(异常前)
{
Console.WriteLine(ex.Message);
}

}



我也用: -
File.Copy(文件路径\\192.168.1.3\upload\);



下面的行不执行...

 字节[] = arrReturn client.UploadFile(阿迪,文件路径); 



试图改变它: -

 字节[] = arrReturn client.UploadFile(\\\\192.168.1.3\\upload\\,文件路径); 



它仍然无法正常工作...任何解决这一问题的?



基本上,我想没有
将文件从客户端转移到文件存储服务器实际上登录到服务器,使客户端不能访问服务器上的存储位置
。直接



这是错误我得到: -


中的Web客户端请求期间发生异常< DIV CLASS =h2_lin>解决方案

从一个文件夹将文件复制到不同的文件夹是很容易的。

下面的代码是在C#.NET。

首先添加System.IO和System.NET你的命名空间。然后添加以下代码。

 字符串_pathForImages =C:\inetpub\wwwroot\NewFolder\ExistingFolder\Images\ 

{
的String [] F = Directory.GetFiles(_pathForImages);
INT K = f.Length;
串_pathForImages_dest =C:\inetpub\wwwroot\NewFolder\NewFolder1\Images\

的for(int i = 0; I< k;我++)
{
变种KL = F [I] .Split('\\');

串FNAME = KL [kl.Length - 1];
串J = _pathForImages_test;
System.IO.File.Copy(F [I],_pathForImages_dest + FNAME);


}
}
赶上(异常前)
{

}




如果您想复制新文件,并替换现有文件只需添加真到file.copy。完整的代码是:

 字符串_pathForImages =C:\inetpub\wwwroot\NewFolder\ ExistingFolder\Images\; 

{
的String [] F = Directory.GetFiles(_pathForImages);
INT K = f.Length;
串_pathForImages_dest =C:\inetpub\wwwroot\NewFolder\NewFolder1\Images\

的for(int i = 0; I< k;我++)
{
变种KL = F [I] .Split('\\');

串FNAME = KL [kl.Length - 1];
串J = _pathForImages_test;
System.IO.File.Copy(F [I],_pathForImages_dest + FNAME,真);


}
}
赶上(异常前)
{

}


Using the link below, I wrote a code for my application. I am not able to get it right though, Please refer the link and help me ot with it...

http://stackoverflow.com/questions/263518/c-uploading-files-to-file-server


The following is my code:-

protected void Button1_Click(object sender, EventArgs e)
{

    filePath = FileUpload1.FileName;    
    try
    {
        WebClient client = new WebClient();

        NetworkCredential nc = new NetworkCredential(uName, password);

        Uri addy = new Uri("\\\\192.168.1.3\\upload\\");
        client.Credentials = nc;
        byte[] arrReturn = client.UploadFile(addy, filePath);

        Console.WriteLine(arrReturn.ToString());
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }

}

I also used:- File.Copy(filePath, "\\192.168.1.3\upload\");

The following line doesn't execute...

byte[] arrReturn = client.UploadFile(addy, filePath);

tried changing it to:-

byte[] arrReturn = client.UploadFile("\\\\192.168.1.3\\upload\\", filePath);

IT still doesn't work...Any solution to it??

I basically want to transfer a file from the client to the file storage server without actually login into the server so that the client cannot access the storage location on the server directly.

THIS IS THE ERROR I GET :- "An exception occurred during a WebClient request"

解决方案

Copying files from one folder to a different folder is very easy.
The below code is in C#.NET.
First add System.IO and System.NET to your namespace. Then add the following code.

string _pathForImages = "c:\inetpub\wwwroot\NewFolder\ExistingFolder\Images\";
   try
    {
        string[] f = Directory.GetFiles(_pathForImages);
        int k = f.Length;
        string _pathForImages_dest = "c:\inetpub\wwwroot\NewFolder\NewFolder1\Images\";

        for (int i = 0; i < k; i++)
        {
            var kl = f[i].Split('\\');

            string fname = kl[kl.Length - 1];
            string j = _pathForImages_test;
            System.IO.File.Copy(f[i], _pathForImages_dest + fname);


        }
    }
    catch (Exception ex)
    {

    }


If you want to copy new files and REPLACE existing files just add 'true' to the file.copy . The full code is:

string _pathForImages = "c:\inetpub\wwwroot\NewFolder\ExistingFolder\Images\";
   try
    {
        string[] f = Directory.GetFiles(_pathForImages);
        int k = f.Length;
        string _pathForImages_dest = "c:\inetpub\wwwroot\NewFolder\NewFolder1\Images\";

        for (int i = 0; i < k; i++)
        {
            var kl = f[i].Split('\\');

            string fname = kl[kl.Length - 1];
            string j = _pathForImages_test;
            System.IO.File.Copy(f[i], _pathForImages_dest + fname,true);


        }
    }
    catch (Exception ex)
    {

    }

这篇关于上传文件到文件服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆