Upload files to a remote server

2020-04-20 11:50发布

I need to upload files from my asp.net (C#) page residing in the web server to a remote server.

I managed to upload files to remote server from localhost using this code:

      string serverPath = "\\\\xx.xxx.xx.xx\\Folder\\" + FileUpload1.FileName;
      FileUpload1.PostedFile.SaveAs(serverPath);

But after I published this code to my web server, it stopped working with the error "The network path was not found."

I have looked at a few solutions which suggest using UNC network share and implementing impersonation. I couldn't figure out how to apply these solutions.

Can someone please give an example, or suggest a simpler solution.

Thanks!!

标签: c# asp.net
3条回答
等我变得足够好
2楼-- · 2020-04-20 12:16

You need a virtual directory on your webserver to upload to. In code you'll have to use Server.Mappath("virtual path") function to get its server path and then save to it.

查看更多
相关推荐>>
3楼-- · 2020-04-20 12:21

In FileUpload1.PostedFile.SaveAs(path), path is physical path of file, No Url. You must check:

  • is Physical folder Exsist?
  • is You have access to folder?

if answer of both question is true check this code:

string serverPath = @"\\xxx.xxx.xxx.xxx\Folder\";
if (!System.IO.Directory.Exists(serverPath))
    System.IO.Directory.CreateDirectory(serverPath);

FileUpload1.PostedFile.SaveAs(serverPath + FileUpload1.FileName);
查看更多
干净又极端
4楼-- · 2020-04-20 12:32

The account your application runs under must have write permissions to the folder you are trying to upload the file to: \\xx.xxx.xx.xx\Folder\. So you will have to configure the application pool in IIS to run under an account that will have sufficient permissions. Go to the application pool properties in the IIS management console where you will be able to specify an account to be used to run the application. By default it uses a built-in account which won't have any access to shared resources. Take a look at the following article which explains how to do so.

查看更多
登录 后发表回答