请不要将其标记为重复问题。我知道如何在上传到网络服务器之前预览图像&我试过以下代码。但是,我如何在Web服务器上传相同的文件,因为在回发文件上传丢失后选择文件值。我只是想知道它将如何上传到服务器&如果选择了多个文件?
<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代码
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
它还有一个按钮来上传文件。
答案 0 :(得分:0)
您可以尝试使用
停止回发<asp:FileUpload ID="FileUpload1" runat="server" OnClientClick="return false;"/>
以避免丢失最近加载的图像
或者将图像保留在Session对象中:
'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