0

私のコードのこの部分は、アプリケーションが targetpath と同じドメインで動作している場合に機能します。ただし、アプリケーションがドメイン外で動作し、ファイルをドメイン内のフォルダーにコピーできるようにする必要があります。

フォルダにアクセスするにはユーザーとパスワードが必要なため、ファイルをアップロードしようとしても機能しません。ドメイン外で targetpath を開こうとすると、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());
        }
    }
4

2 に答える 2

0

UploadFiles.On.External.Domainナゲット パッケージを使用できます。

  1. サービスの構成:
services.AddSingleton<IUpLoadFile, UpLoadFile>();
  1. 次のコードを 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.",

  1. コンストラクターに IUpLoad を挿入します。
private readonly IUpLoadFile UpLoadFile; 
public testController(IUpLoadFile iUpLoadFile)  { UpLoadFile = upLoadFile; } 
  1. 関数を使用する
     I- UpLoadFile.UpLoad(); 
    II- UpLoadFile.UpLoad2(); 
   III- UpLoadFile.Delete();
于 2022-02-22T04:42:06.433 に答える