保存为更改输入文件

时间:2015-04-16 08:08:56

标签: jquery vb.net html5

早上好,

我有一个名为UploadIMG.aspx的文件,其中包含一些代码html和UploadIMG.aspx.vb,这个文件为空。

html看起来像这样:

<div class="wrapper">
    <h3>Click to add</h3>
</div>

我的jquery看起来像这样:

$("h3").click(function () {
$(".wrapper").append("\
   <img id='uploadPreview1' width='200' height='100' /> \
   <input id='MyFile' class='inputImage' type='file' onchange='UploadImage();'/>");
});

我做了一个小提琴:https://jsfiddle.net/bd4j9v4r/

我想将图像上传到我的服务器,只要他们在输入上更改它而不按任何按钮。

我尝试使用SaveAs保存它,但我不知道如何将其应用于onchange。

<script runat="server">
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If (MyFile.HasFile) Then
        Dim fileName As String = MyFile.FileName
        MyFile.PostedFile.SaveAs(Server.MapPath("~/img/") & MyFile.FileName)
    Else
    End If
End Sub
</script>

任何人都可以帮助我吗?感谢

1 个答案:

答案 0 :(得分:1)

您可以使用此更改:

$(document).on('change', '.inputImage', function(){
   console.log(this.value);
   // now here you can upload this file.
});

如果您对使用ajax的文件上载感兴趣:

$(document).on('change', '.inputImage', function(){
   console.log(this.value);
   var formData = new FormData();
   $.ajax({
       url:'', // place your controller's method
       type:'post',
       dataType:'', // text, json etc
       contentType:false,
       processData:false,
       success:function(data){},
       error:function(err){}
   });
});