如何区分文件与2个不同的文件上传控件 [英] How do I differentiate file from 2 different file upload control
问题描述
我正在开发一个vb Web应用程序。其中一个是将图像上传到db中的库中。我有两个不同的FileUpload控件:FileUpload1和FileUpload2。
FileUpload1用于专辑封面(图片)
FileUpload2用于专辑中的图片(多图片)
来自 FileUpload1 和 FileUpload2 的图像将存储在同一个表中,但 FileUpload1 将包含一个额外的标志,将确定表中的图像是否是专辑封面
以下是我目前的fileUpload2代码。它工作正常。但是我很难区分文件上传控件的图像,以便我可以放置标志
我试过的:
Hi,
I'm currently developing a vb web application. One of them is uploading image into the gallery in db. I have two different FileUpload control: FileUpload1 and FileUpload2.
FileUpload1 is for album cover (image)
FileUpload2 is for the images in the album (multi image)
Both images from the FileUpload1 and FileUpload2 will be stored in the same table except that FileUpload1 will have an additional flag which will determine either the image in the table is the album cover or not
Below is the my current code for fileUpload2. It's working fine. However I have trouble to differentiate the image from which file upload control so that I can put the flag
What I have tried:
Dim hfc As HttpFileCollection = Request.Files
Dim imagePath As String = FileUpload2.PostedFile.FileName
Dim imagesize As String
Dim imagename As String = Path.GetFileName(imagePath)
Dim ext As String
Dim contenttype As String = String.Empty
Try
For i As Integer = 0 To hfc.Count - 1
Dim hpf As HttpPostedFile = hfc(i)
ext = Path.GetExtension(hpf.FileName)
imagesize = hpf.ContentLength
If hpf.ContentLength > 0 Then
'Set the contenttype based on File Extension
Select Case ext
Case ".jpg"
contenttype = "jpeg"
Exit Select
Case ".jpeg"
contenttype = "jpeg"
Exit Select
Case ".png"
contenttype = "png"
Exit Select
End Select
If contenttype <> String.Empty Then
Dim fs As Stream = hpf.InputStream
Dim br As New BinaryReader(fs)
Dim bytes As Byte() = br.ReadBytes(fs.Length)
ViewState("imageName") = hpf.FileName
ViewState("imageType") = contenttype
ViewState("imageContent") = bytes
ViewState("imageLength") = imagesize
insertData()
Else
lblMsg.ForeColor = System.Drawing.Color.Red
lblMsg.Text = "Photo extension " & ext & " is not accepted."
End If
End If
Next i
推荐答案
我想(不确定)你我总是使用全局变量。
我还假设你从FileUpload1或FileUpload2调用(或想调用)方法'insertData'。
所以......我建议你考虑使用参数化方法调用。这将允许你告诉被调用的方法以不同的方式行动。
也许你重新思考你的编程风格......
I suppose (not sure) that you work with global variables all the time.
I also suppose that you call (or want to call) the Method 'insertData' either from FileUpload1 or FileUpload2.
So ... I suggest you think about working with parameterized Method-calls. This will allow you 'tell' the called method to act in different ways.
Perhaps you re-think your programming-style ...
这篇关于如何区分文件与2个不同的文件上传控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!