使用ASP.net 4.0在ASPX页面上使用它
<input type="file" id="File1" name="myfiles" multiple="multiple" runat="server">
我想在VB代码中使用这样的东西
For Each File in File1
File1.PostedFile.SaveAs(savePath & File1.Value)
但是,我无法弄清楚它的语法。谷歌搜索结果只有jquery插件和脚本,我不希望所有这些,如果我可以让它与我已经使用它。
有什么想法吗?
答案 0 :(得分:0)
这实际上是一件非常简单的事情。
ASP.NET 4.0-方法..
您的文件上传器的HTML标记应为:
<input type="file" multiple="multiple" id="file1" runat="server" />
提交代码后,您可以使用以下逻辑访问发布的文件:
Dim f As HttpPostedFile
For i As UInt16 = 0 To Request.Files.Count - 1
f = Request.Files(i)
'Your logic here....
Next
ASP.NET 4.5+方法......
您的文件上传器的HTML标记应为:
<asp:FileUpload ID="file1" runat="server" AllowMultiple="true" />
然后,在提交表单时,只需使用类似于:
的代码For Each f In file1.PostedFiles
'Your logic here..
Next
该逻辑将允许您遍历通过控件上传的每个文件。每个“PostedFile”的类型为“HttpPostedFile”,它将为您提供名称,内容长度,扩展名和文件字节的各种属性。
希望这会有所帮助,欢迎来到StackOverflow!
答案 1 :(得分:0)
是的,这看起来更简单:
<td>
<asp:FileUpload runat="server" ID="FileUpload1" AllowMultiple="true" name="files[]" onchange="handleFileSelect(this)" />
<asp:HiddenField runat="server" ID="filesToIgnore" />
<div style="float:right;">
<asp:Button runat="server" ID="btnAddSubArticle" Text="Aggiungi" />
</div>
</td>
然后javascript到你的东西,例如来自muy之前的项目:
<!-- The necessary scripts that do work son. -->
<script type='text/javascript'>
function handleFileSelect(evt) {
// FileList object
//var files = evt.target.files;
var files = evt.files;
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) { continue; }
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
// Render thumbnail.
var span = document.createElement('span');
span.id = "span-img";
// Creates a thumbnail with an onclick function that will handle the mock deletion
span.innerHTML = ['<img data-id="img' + new Date().getTime() + '" class="thumb" src="', e.target.result,
'"title="', escape(theFile.name), '"onclick="deleteImage(this);" />'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
//The image was clicked delete it
function deleteImage(imgToDelete) {
try {
var fu = document.getElementById('<%=filesToIgnore.ClientID.ToString%>')
fu.value += (imgToDelete.title + ",");
imgToDelete.style.display = 'none';
}
catch (err)
{ alert(err); }
}
</script>
然后是服务器端(这里有一些代码可以看一下这个并根据你的需要修改它)
Dim NewLine As String = "<br/>"
Dim ListFiles As New List(Of String)
Dim ignoredFiles As String() = filesToIgnore.Value.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
For Each el In FileUpload1.PostedFiles
If Not ignoredFiles.Any(Function(x) el.FileName.Contains(x)) Then
Select Case Path.GetExtension(el.FileName.ToString.ToLower)
Case ".jpg", ".png", ".jpeg", ".tiff", ".bmp", ".gif"
Try
Dim P As String = Server.MapPath("~/App_Data/temp/img/") & el.FileName.ToString
el.SaveAs(P)
Dim F As New FileInfo(P)
'Dim NewName As String = Guid.NewGuid.ToString & F.Extension.ToString
'ListFiles.Add(NewName)
'*****************************AZIONI DI RIDIMENSIONAMENTO E WATERMARK*******************************'
' Dim img As New Bitmap(P)
Dim imgResizer As New ProtectedImage(Server.MapPath("~/App_Data/temp/img/"), P, Server.MapPath("~/App_Data/temp/img/th/"), Server.MapPath("~/App_Data/temp/img/w/watermark.png"), Server.MapPath("~/App_Data/temp/img/w/watermark.png"), 400, 400, 80, True, GetNumber, ddlDomainMicroArticle.SelectedItem.ToString)
imgResizer.GenerateImages()
If imgResizer.StateWork = ProtectedImage.workState.WorkCompleted Then
lbl.Text = "Watermark e Resize completati" & NewLine
Else
lbl.Text = "Errore durante l'elaborazione dell'immagine" & NewLine
End If
'*****************************AZIONI DI RIDIMENSIONAMENTO E WATERMARK*******************************'
Dim ResizedImage As String = imgResizer.FileList.Item(1).ToString
Dim Thumbnails As String = imgResizer.FileList.Item(2).ToString
ListFiles.Add(ResizedImage)
ListFiles.Add(Thumbnails)
If MoveToCDN(ResizedImage, Thumbnails) Then
lbl.Text = lbl.Text & el.FileName & " Caricata" & NewLine
Else
lbl.Text = lbl.Text & el.FileName & " Fallita" & NewLine
End If
imgResizer.Dispose()
' Elimina l 'eventuale watermark se esiste
If File.Exists(Server.MapPath("~/App_Data/temp/img/w/watermark.png")) Then
File.Delete(Server.MapPath("~/App_Data/temp/img/w/watermark.png"))
End If
If File.Exists(Server.MapPath("~/App_Data/temp/img/" & Replace(ResizedImage, "rs_", ""))) Then
File.Delete(Server.MapPath("~/App_Data/temp/img/" & Replace(ResizedImage, "rs_", "")))
End If
If File.Exists(Server.MapPath("~/App_Data/temp/img/th/" & Replace(Thumbnails, "th_", ""))) Then
File.Delete(Server.MapPath("~/App_Data/temp/img/th/" & Replace(Thumbnails, "th_", "")))
End If
If File.Exists(P) Then
File.Delete(P)
End If
Catch ex As Exception
lbl.Text = lbl.Text & ex.Message & NewLine
End Try
Case Else
lbl.Text = lbl.Text & el.FileName.ToString & " Estensione file non consentita" & NewLine
End Select
End If
Next
我希望这很有用