Asp.net If(fileUpload.HasFile)=false
大家好,这里是代码,然后我将解释我的问题:-Asp.net If(fileUpload.HasFile)=false,asp.net,vb.net,file-upload,Asp.net,Vb.net,File Upload,大家好,这里是代码,然后我将解释我的问题:- Protected Sub btnupload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click If (IsPostBack) Then HandleUploadedFile() Dim savePath As String = "Images\" If (fileUpload
Protected Sub btnupload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If (IsPostBack) Then
HandleUploadedFile()
Dim savePath As String = "Images\"
If (fileUpload.HasFile) Then
Dim fileName As String = fileUpload.FileName
savePath = Server.MapPath(savePath) + fileName
fileUpload.SaveAs(savePath)
Me.Label1.Text = "Your file was saved as " & fileName
adp.Insert(fileUpload.FileName)
Else
Me.Label1.Text = "You did not specify a file to upload."
End If
End If
End Sub
每次我跟踪代码时,它都会说(fileUpload.HasFile)=false
,之后再也看不到代码,然后跳到else语句,这就是代码的其余部分
Private Sub HandleUploadedFile()
' get the root of the web site
Dim root As String = Server.MapPath("~/")
' clean up the path
If Not root.EndsWith("\") Then
root += "\"
End If
' make a folder to store the images in
Dim fileDirectory As String = root & "Images/"
' create the folder if it does not exist
If Not System.IO.Directory.Exists(fileDirectory) Then
System.IO.Directory.CreateDirectory(fileDirectory)
End If
' make a link to the new file
Dim link As String = "<a href='Images/{0}' target='_blank'>{1}</a>{2}{3}"
' loop through the file in the request
For i As Integer = 0 To Request.Files.Count - 1
' get the file instance
Dim fi As HttpPostedFile = Request.Files.[Get](i)
' create a byte array to store the file bytes
Dim fileBytes As Byte() = New Byte(fi.ContentLength - 1) {}
' fill the byte array
Using stream As System.IO.Stream = fi.InputStream
stream.Read(fileBytes, 0, fi.ContentLength)
End Using
' create a random file name
Dim fileName As String = Guid.NewGuid().ToString()
'write the original file to the file system
File.WriteAllBytes(fileDirectory + fileName & ".jpg", fileBytes)
litText.Text += String.Format(link, fileName & ".jpg", fileName & " Original", "<br/>", "")
' write the resized file to the file system
File.WriteAllBytes(fileDirectory + fileName & "_small.jpg", ResizeImageFile(fileBytes, 100))
litText.Text += String.Format(link, fileName & "_small.jpg", fileName & " Small", "<br/>", "<br/>")
' cleanup
litText.Visible = True
fileBytes = Nothing
Next
End Sub
Private Shared Function ResizeImageFile(ByVal imageFile As Byte(), ByVal targetSize As Integer) As Byte()
Using oldImage As System.Drawing.Image = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
Dim newSize As Size = CalculateDimensions(oldImage.Size, targetSize)
Using newImage As New Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)
Using canvas As Graphics = Graphics.FromImage(newImage)
canvas.SmoothingMode = SmoothingMode.AntiAlias
canvas.InterpolationMode = InterpolationMode.HighQualityBicubic
canvas.PixelOffsetMode = PixelOffsetMode.HighQuality
canvas.DrawImage(oldImage, New Rectangle(New Point(0, 0), newSize))
Dim m As New MemoryStream()
newImage.Save(m, ImageFormat.Jpeg)
Return m.GetBuffer()
End Using
End Using
End Using
End Function
Private Shared Function CalculateDimensions(ByVal oldSize As Size, ByVal targetSize As Integer) As Size
Dim newSize As New Size()
If oldSize.Height > oldSize.Width Then
newSize.Width = CInt((oldSize.Width * (CSng(targetSize) / CSng(oldSize.Height))))
newSize.Height = targetSize
Else
newSize.Width = targetSize
newSize.Height = CInt((oldSize.Height * (CSng(targetSize) / CSng(oldSize.Width))))
End If
Return newSize
End Function
End Class
Private子HandleUploadedFile()
'获取网站的根目录
Dim root作为字符串=Server.MapPath(“~/”)
"清理道路,
如果不是root.EndsWith(\”),则
根+=“\”
如果结束
'创建一个文件夹以存储图像
Dim fileDirectory作为String=root&“Images/”
'如果文件夹不存在,请创建该文件夹
如果System.IO.Directory.Exists(fileDirectory)不存在,则
System.IO.Directory.CreateDirectory(fileDirectory)
如果结束
'创建指向新文件的链接
Dim链接为字符串=“{2}{3}”
'循环浏览请求中的文件
对于i As Integer=0的Request.Files.Count-1
'获取文件实例
Dim fi作为HttpPostedFile=Request.Files。[Get](i)
'创建字节数组以存储文件字节
Dim fileBytes As Byte()=新字节(fi.ContentLength-1){}
'填充字节数组
使用流作为System.IO.stream=fi.InputStream
stream.Read(fileBytes,0,fi.ContentLength)
终端使用
'创建一个随机文件名
Dim文件名为String=Guid.NewGuid().ToString()
'将原始文件写入文件系统
File.writealBytes(fileDirectory+fileName&“.jpg”,fileBytes)
litext.Text+=String.Format(链接、文件名和“.jpg”、文件名和“原件”、“
”和“)
'将调整大小的文件写入文件系统
File.writealBytes(fileDirectory+fileName&“u small.jpg”,ResizeImageFile(fileBytes,100))
litext.Text+=String.Format(链接、文件名和“\u small.jpg”、文件名和“small”、“小于br/>”、“小于br/>”)
"清理",
litext.Visible=True
fileBytes=Nothing
下一个
端接头
专用共享函数将imageFile(ByVal imageFile作为Byte(),ByVal targetSize作为Integer)的大小调整为Byte()
将oldImage用作System.Drawing.Image=System.Drawing.Image.FromStream(新内存流(imageFile))
Dim newSize As Size=计算尺寸(oldImage.Size,targetSize)
将newImage用作新位图(newSize.Width、newSize.Height、PixelFormat.Format24bppRgb)
使用画布作为图形=Graphics.FromImage(newImage)
canvas.SmoothingMode=SmoothingMode.AntiAlias
canvas.InterpolationMode=InterpolationMode.HighQualityBicubic
canvas.PixelOffsetMode=PixelOffsetMode.HighQuality
DrawImage(旧图像,新矩形(新点(0,0),新大小))
Dim作为新的内存流()
newImage.Save(m,ImageFormat.Jpeg)
返回m.GetBuffer()
终端使用
终端使用
终端使用
端函数
专用共享函数CalculateDimensions(ByVal oldSize作为大小,ByVal targetSize作为整数)作为大小
将新闻大小调整为新大小()
如果oldSize.Height>oldSize.Width,则
newSize.Width=CInt((oldSize.Width*(CSng(targetSize)/CSng(oldSize.Height)))
newSize.Height=targetSize
其他的
newSize.Width=targetSize
newSize.Height=CInt((oldSize.Height*(CSng(targetSize)/CSng(oldSize.Width)))
如果结束
返回新闻大小
端函数
末级
我认为可能是您将文件写入目录,这样做会清除文件上载表单。这是在运行if语句之前发生的。尝试将if/else移到“handleuploadedfile()”函数上方。这应确保您可以在系统写入文件并将其从上载表单中删除之前检查上载表单。之所以fileUpload.HasFiles=False
,是因为到达该代码时,文件流已在HandleUploadFile子例程中关闭
'此using语句关闭原始输入流,并将其删除
'从文件上载
Using stream As System.IO.Stream = fi.InputStream
stream.Read(fileBytes, 0, fi.ContentLength)
End Using
检查您是否正在使用UpdatePanel,如果是,请在Page.Init中执行以下操作:
protected void userView_Init(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).RegisterPostBackControl(uploadFileButton);
}
带sql数据库的vb.net这是我们修复它的原因。您可以选择在UpdatePanel的触发器部分中添加PostBackTrigger行,而不是在Init中处理它。