Please Don't mark it as repeated question. I know how to preview image before uploading to web server & I tried as following code. But then how do I upload same file on web server because after postback file upload losses selected files value. I just want to know how will it upload to server & also if multiple files selected?
<asp:FileUpload ID="FileUpload1" runat="server" /><br /><br />
<asp:Image ID="preview1" runat="server" CssClass="preview" />
<asp:Button ID="delete1" runat="server" Text="Delete1" /><br />
<script type="text/javascript">
function uploadPreview(fileUpload) {
if (fileUpload.value != '') {
document.getElementById("<%=Upload.ClientID %>").click();
}
}
</script>
VB code
Private Sub Online_Medicines_order_online_Default2_Load(sender As Object, e As EventArgs) Handles Me.Load
FileUpload1.Attributes("onchange") = "uploadPreview(this)"
End Sub
Protected Sub uploadPreview(sender As Object, e As EventArgs)
Dim fs As System.IO.Stream = FileUpload1.PostedFile.InputStream
Dim br As New System.IO.BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Integer))
Dim base64String As String = Convert.ToBase64String(bytes, 0, bytes.Length)
If preview1.ImageUrl = "" Then
preview1.ImageUrl = "data:image/png;base64," & base64String
preview1.Visible = True
End If
End Sub
It will have one more button to upload a file.
You can try stopping the postback with
<asp:FileUpload ID="FileUpload1" runat="server" OnClientClick="return false;"/>
to avoid losing the recently loaded image
Or retain the image in the Session object:
'If first time page is submitted and we have file in FileUpload control but not in session
'Store the values to SEssion Object
If (Session("FileUpload1") Is Nothing AndAlsdo FileUpload1.HasFile) Then
Session("FileUpload1") = FileUpload1
Label1.Text = FileUpload1.FileName
'Next time submit and Session has values but FileUpload is Blank
ElseIf (Session("FileUpload1") IsNot Nothing AndAlso (Not FileUpload1.HasFile)) Then
FileUpload1 = (FileUpload) Session("FileUpload1")
Label1.Text = FileUpload1.FileName
' Session has File but user want to change the file
ElseIf (FileUpload1.HasFile) Then
Session("FileUpload1") = FileUpload1
Label1.Text = FileUpload1.FileName
End IF
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.