0

Windows コンソール アプリとして C# で記述された既存の CGI アプリケーションに関数を追加する必要があります。少数のユーザーがデータファイルを送信して処理できるようにする機能を追加する必要があります。データファイルは、テキスト/csv ファイルまたはさまざまなバイナリ ファイル (.xls、.pdf) のいずれかです。ファイルを選択/送信するためのフィールドを使用して、簡単なテスト HTML フォームをセットアップしました。それはすべてうまくいきます。問題なくテキスト ファイルをサーバーに保存できます。しかし、バイナリ ファイルを保存するにはどうすればよいでしょうか。簡単にできると思いますが、理解できませんでした。

以下は、テキスト ファイルを保存するためのサンプル コードです。

String formData = Console.In.ReadToEnd();
string boundary = string.Empty;
string[] cPairs = cType.Split(new string[] { "; " }, StringSplitOptions.None);
foreach (string pair in cPairs) {
    //finds the 'boundary' text
    if (pair.Contains("boundary"))
        boundary = "--" + pair.Split('=')[1];
}

//splits on the 'boundary' to get individual form fields/sections
string[] sections = rawParams.Split(new string[] { boundary }, StringSplitOptions.RemoveEmptyEntries);

// parse each section
foreach (string section in sections) {
    string[] parts = section.Split(new string[] { "; ", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    if (parts[1].Equals("name=\"dFile\"")) { // 'dFile' is the form field-name for the datafile
        //below lines get the filename
        Regex regx = new Regex("filename=\"(.+)\"", RegexOptions.IgnoreCase);
        Match fPath = regx.Match(parts[2]);
        FileInfo fi = new FileInfo(fPath.Groups[1].Value);
        regx = new Regex("([-A-Z0-9_ .]+)", RegexOptions.IgnoreCase);
        Match fname = regx.Match(fi.Name);

        //below lines save the file contents to a text file; starts at index 4 of the 'parts' array
        if (fname.Groups[1].Success) {
            TextWriter tw = new StreamWriter(fname.Groups[1].Value);
            for (int i = 4; i < parts.Length; i++) {
                tw.WriteLine(parts[i]);
            }
            tw.Close();
        }
    }
    else {
        // parse other non-file form fields
    }
}

重要な部分は、ファイルへの保存です。バイナリファイルに対してそれを行うにはどうすればよいですか? デイブ

4

1 に答える 1

0

一部のデータをバイナリ ファイルにダンプするだけの場合は、次のようなものを使用します。

FileStream fs = File.Create(fname.Groups[1].Value, SOME_SIZE, FileOptions.None))
BinaryFormatter formatter = new BinaryFormatter();
...
//inside your loop
string s = parts[i];
formatter.Serialize(fs, Encoding.Unicode.GetBytes(s));

明らかGetBytes()に正しいエンコーディングを使用しています。

したがって、コードは次のようになります。

String formData = Console.In.ReadToEnd();
string boundary = string.Empty;
string[] cPairs = cType.Split(new string[] { "; " }, StringSplitOptions.None);
foreach (string pair in cPairs) {
//finds the 'boundary' text
if (pair.Contains("boundary"))
    boundary = "--" + pair.Split('=')[1];
}

//splits on the 'boundary' to get individual form fields/sections
string[] sections = rawParams.Split(new string[] { boundary }, StringSplitOptions.RemoveEmptyEntries);

// parse each section
foreach (string section in sections) {
string[] parts = section.Split(new string[] { "; ", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
if (parts[1].Equals("name=\"dFile\"")) { // 'dFile' is the form field-name for the datafile
    //below lines get the filename
    Regex regx = new Regex("filename=\"(.+)\"", RegexOptions.IgnoreCase);
    Match fPath = regx.Match(parts[2]);
    FileInfo fi = new FileInfo(fPath.Groups[1].Value);
    regx = new Regex("([-A-Z0-9_ .]+)", RegexOptions.IgnoreCase);
    Match fname = regx.Match(fi.Name);

    FileStream fs = File.Create(fname.Groups[1].Value, SOME_SIZE, FileOptions.None))
    BinaryFormatter formatter = new BinaryFormatter();

    //below lines save the file contents to a text file; starts at index 4 of the 'parts' array
    if (fname.Groups[1].Success) {
        for (int i = 4; i < parts.Length; i++) {
            string s = parts[i];
            formatter.Serialize(fs, Encoding.Unicode.GetBytes(s));
        }
    }
}
else {
    // parse other non-file form fields
}

}

于 2012-09-06T15:51:44.093 に答える