DBA

ajax File Upload 본문

[3] Development/MVC(ASP.NET with C#)

ajax File Upload

코볼 2022. 12. 12. 15:25
728x90
반응형
SMALL

[javascript]

<script type="text/javascript">
    $(document).ready(function () {
        $("#btnUpload").on("click", function (e) {
            var formData = new FormData();
            formData.append("uploadfile", $("input[name=uploadFile]")[0].files[0]);
            var jsQuestionnaire = $("#ddlQuestionnaire").val();
            //alert(jsQuestionnaire);
            var jsUploadFileName = "";

            $.ajax({
                url: '/Answer/FileUpload?questionnaireID=' + jsQuestionnaire,
                processData: false,
                contentType: false,
                data: formData,
                type: 'POST',
                success: function (data) {
                    alert("등록 되었습니다.");
                    jsUploadFileName = data.fileName;
                },
                complete: function () {
                    ExcelReadResult(jsUploadFileName, jsQuestionnaire);
                },
                error: function (xhr, status, error) {
                    alert("code: " + xhr.status);
                    alert(error);
                }
            });
        });
    });
</script>

 

[html]

<div class="col-md-6">
    <input id="file" name="uploadFile" type="file">
</div>
<div class="col-md-2">
    <input type="button" id="btnUpload" class="btn btn-md btn-danger btn-block" searchCondition="default" value="업로드" />
</div>

 

 

[cs] 파일명 가져오기

var fileContent = Request.Files["uploadFile"];
var ext = Path.GetExtension(fileContent.FileName);

728x90
반응형
LIST
Comments