0

我需要将一个函数添加到以 C# 编写的现有 CGI 应用程序作为 Windows 控制台应用程序。我需要添加一些用户能够提交数据文件进行处理的能力。数据文件可以是文本/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 回答