How to upload with Jquery - Ajax - Webservice

Asked

Viewed 21 times

-2

Good afternoon guys, I’m having a hard time uploading a file to the server.

I have tested locally and managed to accomplish successfully both on my machine running by the project and on the server published on IIS 10.0. The problem arises when I try to upload a file through my machine, in the application published on the server. Follow the codes I’m using. The error that returns is 500 Internal server error.

JS

function uploadCaminhoArquivo() {
        var btnUpload = document.querySelector("#btnUpload");
        var btnUploadx = document.querySelector("#btnUploadx");
        var dadoUpload = document.getElementById("dadoUpload").files[0].name;
        var tipoaviso = document.getElementById("ddlTipoAviso").value;
        
        var path = dadoUpload;
        var dataForm = new FormData();
        var fileUpload = $('#dadoUpload').get(0);
        var files = fileUpload.files;
        dataForm.append(files[0].name, files[0]);
        dataForm.append('id', id);
        dataForm.append('tipoaviso', tipoaviso);
        $.ajax({
            type: "POST",
            url: "Gerencial.asmx/criarDiretorio",
            data: dataForm,
            contentType: 'multipart/form-data',
            processData: false,
            success: function () {
                console.log("sucesso");
            },
            error: function () {
                console.log("erro");
                $("#msgErrAnexo").fadeIn(500).delay(1000).fadeOut(500);
                dadoUploadX.style.display = "none";
                btnUpload.style.display = "block";
                btnUploadx.style.display = "none";
            }
        })
    }

Webservice

[HttpPost]
    [WebMethod]
    public string criarDiretorio()
    {
        string SQL;
        HttpFileCollection Files = HttpContext.Current.Request.Files;
        string idprocesso = HttpContext.Current.Request.Files["id"];
        string tipoaviso = HttpContext.Current.Request.Files["tipoaviso"];
        string path = HttpContext.Current.Server.MapPath("UPLOADS/");
        HttpPostedFile File = Files[0];
        string fileName = File.FileName;


        SQL = "SELECT IDTIPOAVISO, TPPROCESSO FROM TB_TIPOAVISO WHERE IDTIPOAVISO = '" + tipoaviso + "' ";
        DataTable listTable = new DataTable();
        listTable = DBS.List(SQL);
        string tipoprocesso = listTable.Rows[0]["TPPROCESSO"].ToString();
        string idtipoaviso = listTable.Rows[0]["IDTIPOAVISO"].ToString();

        SQL = "SELECT B.NM_TIPO_ESTUFAGEM, D.NM_VIATRANSPORTE, ";
        SQL += "A.DT_PREVISAO_EMBARQUE, A.DT_PREVISAO_CHEGADA ";
        SQL += "from TB_BL A ";
        SQL += "LEFT JOIN TB_TIPO_ESTUFAGEM B ON A.ID_TIPO_ESTUFAGEM = B.ID_TIPO_ESTUFAGEM ";
        SQL += "LEFT JOIN TB_SERVICO C ON A.ID_SERVICO = C.ID_SERVICO ";
        SQL += "LEFT JOIN TB_VIATRANSPORTE D ON C.ID_VIATRANSPORTE = D.ID_VIATRANSPORTE ";
        SQL += "WHERE A.ID_BL = '" + idprocesso + "' ";
        DataTable verifica = new DataTable();
        verifica = DBS.List(SQL);
        string tipoEstufagem = verifica.Rows[0]["NM_TIPO_ESTUFAGEM"].ToString();
        string viatransporte = verifica.Rows[0]["NM_VIATRANSPORTE"].ToString();
        string previsaoEmbarque = verifica.Rows[0]["DT_PREVISAO_EMBARQUE"].ToString();
        string previsaoChegada = verifica.Rows[0]["DT_PREVISAO_CHEGADA"].ToString();

        if (listTable.Rows[0]["TPPROCESSO"].ToString() == "P")
        {

            SQL = "SELECT M.NR_BL as NRMASTER, C.NR_PROCESSO AS NRHOUSE FROM TB_BL C LEFT JOIN TB_BL M ON C.ID_BL_MASTER = M.ID_BL WHERE C.ID_BL = '" + idprocesso + "' ";
            DataTable listTable2 = new DataTable();
            listTable2 = DBS.List(SQL);
            string anoH = listTable2.Rows[0]["NRHOUSE"].ToString().Substring(9, 2);
            string mesH = listTable2.Rows[0]["NRHOUSE"].ToString().Substring(6, 2);
            string diretorio = path+"20" + anoH + "\\" + mesH + "\\" + listTable2.Rows[0]["NRHOUSE"].ToString().Replace("/", "") + "\\";

            if (Directory.Exists(diretorio) == false)
            {
                DirectoryInfo di = Directory.CreateDirectory(diretorio);
            }

            File.SaveAs(Path.Combine(diretorio, fileName));
            
            return JsonConvert.SerializeObject("0");
        }
        else
        {
            SQL = "SELECT M.NR_BL as BL_MASTER, C.NR_PROCESSO AS NRHOUSE FROM TB_BL C LEFT JOIN TB_BL M ON C.ID_BL_MASTER = M.ID_BL WHERE C.ID_BL = '" + idprocesso + "' ";
            DataTable listTable2 = new DataTable();
            listTable2 = DBS.List(SQL);
            string anoH = listTable2.Rows[0]["NRHOUSE"].ToString().Substring(9, 2);
            string mesH = listTable2.Rows[0]["NRHOUSE"].ToString().Substring(6, 2);
            string blmaster = listTable2.Rows[0]["BL_MASTER"].ToString();
            string diretorio = path+"20" + anoH + "\\" + mesH + "\\MASTER-" + blmaster + "\\";

            if (Directory.Exists(diretorio) == false)
            {
                DirectoryInfo di = Directory.CreateDirectory(diretorio);
            }

            File.SaveAs(Path.Combine(diretorio, fileName));

            return JsonConvert.SerializeObject("0");
        }
    }

Write permissions on IIS: https://i.stack.Imgur.com/xxwr9.png

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.