由 de 域外的用户将文件上传到域内的文件夹
本文关键字:文件夹 de 用户 文件 | 更新日期: 2023-09-27 18:37:28
如果应用程序与目标路径在同一个域中工作,我的这部分代码就可以工作。但是我需要应用程序在域外工作,并且可以将文件复制到域内的文件夹中。
当我尝试上传文件不起作用时,因为文件夹访问需要用户和密码才能访问该文件夹。如果我尝试打开域外的目标路径,Windows 会要求输入凭据。
那么,如何在代码上传递用户和密码呢?
private void btnAnexar_Click(object sender, EventArgs e)
{
String input = string.Empty;
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter ="All files (*.*)|*.*";
dialog.InitialDirectory = "C:";
dialog.Title = "Select a text file";
if (dialog.ShowDialog() == DialogResult.OK)
input = dialog.FileName;
try
{
string fileName = dialog.SafeFileName;
string extension = Path.GetExtension(fileName);
string sourcePath = Path.GetDirectoryName(input);
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
copia = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + extension;
MakeUnique(targetPath + "''"+ copia);
string destFile = System.IO.Path.Combine(targetPath, copia);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
linkLabel1.Text = "...''" + copia;
if (input == String.Empty)
return; //user didn't select a file to open
}
catch (Exception e3)
{
Console.WriteLine(e3.ToString());
}
}
运行程序的用户需要对目标文件夹具有写入权限。您需要是有权更改目标目录的读/写权限的用户,或者具有此类用户的凭据才能在代码中执行此操作。
您可以使用 UploadFiles.On.External.Domain nugget 包:
- 配置服务 :
services.AddSingleton<IUpLoadFile, UpLoadFile>();
- 在appsettings.json中编写此代码:
"UploadFileSettings": {
"Domain": "yourdomain.com",
"Root": "root//www//files,
"Separator":"//",
"MaximumSize": 5
},
"Domain": "your domain name",
"Root": "your server root",
"Separator":"{for Windows //}, {for Linux ''}",
"MaximumSize": "number by MB.",
- 在构造函数中注入 IUpLoad:
private readonly IUpLoadFile UpLoadFile;
public testController(IUpLoadFile iUpLoadFile) { UpLoadFile = upLoadFile; }
- 使用函数
I- UpLoadFile.UpLoad();
II- UpLoadFile.UpLoad2();
III- UpLoadFile.Delete();