【问题标题】:Send XLSX file to controller with C# and JavaScript使用 C# 和 JavaScript 将 XLSX 文件发送到控制器
【发布时间】:2022-08-06 23:30:43
【问题描述】:

我需要向Controller 发送一个XLSX 文件和一个HTML 文件,但我不知道Model 的文件是什么类型(字符串、字符、字节[]...) ,我怎样才能传递这些文件?

我的 HTML 如下所示:

                <div class=\"flex flex-column\">
                    <!--Para inserir o titulo do email-->
                    <h6>Titulo:</h6>
                    <input id=\"titulo\" type=\"text\" placeholder=\"Titulo do Email\" style=\"margin: 0 !important; width: 280px;\" />
                </div>
                <div class=\"flex flex-column\">
                    <!--Para inserir o corpo do email-->
                    <h6>Anexar corpo do email:</h6>
                    <input type=\"file\" id=\"corpo\" />
                </div>
                <div class=\"flex flex-column\">
                    <!--Para parametro e os emails-->
                    <h6>Importar planilha de e-mails:</h6>
                    <input type=\"file\" id=\"parametros\" />
                </div>
            </div>
            <div class=\"button-space\">
                <input class=\"btn btn-primary\" type=\"button\" value=\"Preparar e-mails\" onclick=\"CarregarDados()\" />
            </div>

JavaScript:

    function EnviarEmail() {
        const data = {
            Titulo: document.getElementById(\"titulo\").value,
            Corpo: document.getElementById(\"corpo\").value,
            Parametros: document.getElementById(\"parametros\").value
        };
    
        console.log(data);
        //POST request with body equal on data in JSON format
        fetch(\'/DisparoEmail/EnviaEmail\', {
            method: \'POST\',
            headers: {
                \'Content-Type\': \'application/json\',
            },
            body: JSON.stringify(data),
        })
        .then((response) => response.arrayBuffer())
        //Then with the data from the response in JSON...
        .then((buffer) => {
    
            const decoder = new TextDecoder(\'iso-8859-1\');
            const text = decoder.decode(buffer);
    
    
            //document.querySelector(\"#divListaFila\").innerHTML = text;
        })
        //Then with the error genereted...
        .catch((error) => {
            console.error(\'Error:\', error);
        });

}

型号:

public class DisparoEmailViewModel
{
    public string Titulo { get; set; }
    public string Corpo { get; set; }
    public string Parametros { get; set; }
    List<Parametro1> Parametro1s { get; set; }
    List<Parametro2> Parametro2s { get; set; }

}

和控制器:

public bool EnviaEmail(DisparoEmailViewModel email = null)
{
    if(email != null)
    {
        DisparoEmailExtension teste = new DisparoEmailExtension();
        teste.EnviaEmail(email);
        return true;
    }
    return false;
}

    标签: c# .net xlsx


    【解决方案1】:

    您应该使用HttpPostedFileBase 处理文件上传(请参阅this link)。

    此外,如果您需要支持多个文件,您可以使用&lt;input type='file' multiple&gt;(请参阅this link)。

    【讨论】:

      猜你喜欢
      • 2014-06-24
      • 2022-01-16
      • 2019-08-05
      • 2020-08-21
      • 2016-11-24
      • 2020-11-04
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      相关资源
      最近更新 更多