iT邦幫忙

0

請問如何將aspx的asp:FileUpload藉由ajax傳到vb.net類別庫的controller內

各位大大您好:
請問如何將aspx的asp:FileUpload藉由ajax傳到vb.net類別庫的controller內。
aspx程式碼: (使用vue,iview)

<asp:FileUpload ID="FileUpload1" runat="server" />

<i-Button type="primary" @click="Save" >送出</i-Button>

<script>
        new Vue({
            el: '#app',
......

methods: {
 , Save: function () {
                    const vm = this;
                    console.log(vm);
                    vm.origin = window.location.origin;
                    if (vm.PLANE == '') {
                        alert('飛機必填');
                        this.$refs.PLANE.focus();
                        return;
                    }
                    if (vm.QUESTION == '') {
                        alert('問題必填');
                        this.$refs.Tel2.focus();
                        return;
                    }
                    if (vm.WONDERWOMAN == '') {
                        alert('神力女超人必填');
                        return;
                    }

                    var ELECTRIC = $("#ELECTRIC").val();

                    var File = document.getElementById("FileUpload1");
                    console.log(File);
                    console.log(File.files[0]);
                    var userid = "<%=USERID%>";
                    var Parameter = {
                        NO: "",
                        KIND: "A",
                        PLANE: vm.PLANE,
                        ENGINE: vm.ENGINE,
                        BRAKES: vm.BRAKES,
                        QUESTION: vm.QUESTION,
                        WONDERWOMAN: vm.WONDERWOMAN,
                        AUTOPILOT: "1",
                        REPLY: "",
                        HEADLIGHT: userid ,
                        Files: File.files,
                        ELECTRIC: ELECTRIC
                    };
                    console.log(Parameter);
                    vm.loading = true;
                    axios.post(vm.origin + '/DCAPI/CONSTRUCTION/DEMO/DATASAVE', Parameter, {
                        headers: {
                            'Content-Type': 'application/json',
                        }
                    }).then(function (response) {
                        console.log(response);
                        if (response.data.STATE == 'SUCCESS') {
                            console.log(response.data.MSG);
                            alert("儲存成功");
                            vm.PLANE = "";
                            vm.ENGINE = "";
                            vm.BRAKES = "";
                            vm.QUESTION = "";
                            vm.WONDERWOMAN = "";
                           
                        } else {
                            alert(response.data.MSG);
                        }

                    }).catch(function (error) {
                        alert('失敗' + error.message);
                    }).finally(function () {
                        vm.loading = false;
                    });

                }
}
</script>

以上程式碼是在aspx在asp:FileUpload選取資料後,點選送出,接著到function Save
經由ajax將參數送到 '/DCAPI/CONSTRUCTION/DEMO/DATASAVE'
其中
NO: "",
KIND: "A",
PLANE: vm.PLANE,
ENGINE: vm.ENGINE,
BRAKES: vm.BRAKES,
QUESTION: vm.QUESTION,
WONDERWOMAN: vm.WONDERWOMAN,
AUTOPILOT: "1",
REPLY: "",
HEADLIGHT: userid ,
ELECTRIC: ELECTRIC
以上參數均傳送成功
只有Files傳送失敗
Files: File.files,
Controller.vb 程式碼:

<HttpPost>
        Public Function DATASAVE(<FromBody()> ByVal TOBJ As DATASAVEPARA) As DATASAVEOUT
            Dim steps As String = "0"
            Dim DEMODao As New DEMODao
            Dim TOUT As New DATASAVEOUT
            Dim strTempPath As String = ""
            Dim filePathName As String = ""
            Dim testi As String = "0"



            Try
                testi += TOBJ.Files(0).FileName

            Catch ex As Exception
                TOUT.STATE += "FAIL_DEMOController"
                TOUT.MSG += "FAIL_DEMOController" + ex.Message + ",steps=" + steps + ",strace" + ex.StackTrace + ",testi=" + testi
            End Try

            Return TOUT

        End Function

傳入Controller的MODEL程式碼:

Public Class DATASAVEPARA
    Public KIND As String    '傳參數成功
    Public NO As String       '傳參數成功
    Public PLANE As String       '傳參數成功
    Public ENGINE As String       '傳參數成功
    Public BRAKES As String       '傳參數成功
    Public QUESTION As String       '傳參數成功
    Public WONDERWOMAN As String       '傳參數成功
    Public AUTOPILOT As String       '傳參數成功
    Public REPLY As String       '傳參數成功
    Public HEADLIGHT As String       '傳參數成功
    Public MDATE As String       '傳參數成功
    Public ELECTRIC As String       '傳參數成功
    Public Files As HttpPostedFile()       '傳參數失敗   並未將物件參考設定為物件的執行個體
End Class

無論將DATASAVEPARA的HttpPostedFile() 替換成以下方式或加上Property均失敗
HttpPostedFile
List(of HttpPostedFile)

HttpPostedFileBase
HttpPostedFileBase()
List(of HttpPostedFileBase)

IFormFile 'Imports Microsoft.AspNetCore.Http
IFormFile() 'Imports Microsoft.AspNetCore.Http
List(of IFormFile) 'Imports Microsoft.AspNetCore.Http

Object
Object()
List(of Object)

new List(of HttpPostedFileBase) '不會產生錯誤但是參數沒有任何值
new List(of IFormFile) 'Imports Microsoft.AspNetCore.Http'不會產生錯誤但是參數沒有任何值
new List(of Object)'不會產生錯誤但是參數沒有任何值

請問各位大大,如何將aspx的files成功傳入到vb.net的controller內,謝謝!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
天黑
iT邦研究生 5 級 ‧ 2022-01-21 15:47:50

也許你需要的調整axios post的資料類型

請參考

我要發表回答

立即登入回答