如何在ASP.NET中使用具有多个文件上载控制器(如fileupload1,fileupload2,fileupload3)的多个文件上载(任何文件) [英] How to use multiple files uploading(any files) with multiple file upload controllers(like fileupload1, fileupload2, fileupload3) in ASP.NET
问题描述
我想保存文件上载控制器各自的数据库中的所有文件路径。我的意思是当我从fileupload1中选择3个文件时,这些文件作为带有文件路径的记录存储在一个表中,id.Same就像存储的fileupload2文件一样另一种形式与Path,id.I is usinf 7文件上传控制器单击按钮。
我尝试过:
.Aspx代码
< html xmlns =http://www.w3.org/1999/xhtml>
< head runat =server>
< title> jQuery多文件上传< / title>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js>< / script>
< style type =text / css>
#divFile p {
字体:13px tahoma,宋体;
}
#divFile h3 {
字体:16px arial,tahoma;
font-weight:bold;
}
< / style>
< script>
$('#btUpload')。click(function(){
if(fileUpload.value.length == 0){// CHECK IF FILE(S)SELECTED。
alert('没有文件被选中。');
返回false;
}
}); < br $>
< / script>
< script>
$('#btUpload')。click(function(){
if(fileUpload1.value.length == 0){// CHECK IF FILE(S)SELECTED。
alert('没有文件被选中。');
返回false;
}
});
< / script>
< / head>
< body>
< form id =form1runat =server>
< asp:FileUpload ID =fileUploadmultiple =truerunat =server/>
< asp:Button ID =btUploadText =上传文件
OnClick =Upload_Filesrunat =server/>
< asp:label id =lblFileListrunat =server>
< asp:标签ID =lblUploadStatusrunat =server>
< asp:Label ID =lblFailedStatusrunat =server>
< asp:FileUpload ID =fileUpload1multiple =truerunat =server/>
< asp:lab el id =lblFileList1runat =server>
< asp:Label ID =lblUploadStatus1runat =server>
< asp:Label ID =lblFailedStatus1runat =server>
< / form>
< / body>
< / html>
.cs代码:
protected void Fileupload()
{
if(fileUpload.HasFile)//检查是否已选择任何文件。
{
int iUploadedCnt = 0;
int iFailedCnt = 0;
HttpFileCollection hfc = Request.Files;
lblFileList.Text =选择+ hfc.Count + file(s);
if(hfc.Count < = 10)// 10 FILES RESTRICTION。
{
for(int i = 0; i< = hfc.Count - 1; i ++)>
{
HttpPostedFile hpf = hfc [i];
if(hpf.ContentLength> 0)
{
if(!File.Exists(Server.MapPath(images \\)) +
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
new DirectoryInfo( Server.MapPath(images \\));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
//检查重复文件。
FileInfo [] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt,)+。*);
if(objFI.Length> 0)
{
//检查具有相同名称的文件是否支持
foreach(FileInfo文件中) objFI)
{
string sFileName1 = objFI [0] .Name;
string sFileExt1 = Path.GetExtension(objFI [0] .Name );
if(sFileName1.Replace(sFileExt1,)==
sFileName.Replace(sFileExt,))
{
iFailedCnt + = 1; //不允许重复。
休息;
}
}
}
else
{
//在文件夹中保存文件。
hpf.SaveAs(Server.MapPath(images \\ )+
Path.GetFileName(hpf.FileName));
iUploadedCnt + = 1;
}
}
}
}
lblUploadStatus.Text =+ iUploadedCnt + file(s)已上传。;
lblFailedStatus.Text =+ iFailedCnt +
无法上传重复的文件。;
}
else lblUploadStatus.Text =Max。允许10个文件。;
}
else lblUploadStatus.Text =没有选择文件。;
}
protected void Fileupload1()
{
if(fileUpload1.HasFile)//检查是否已选择任何文件。
{
int iUploadedCnt1 = 0;
int iFailedCnt1 = 0;
HttpFileCollection hfc1 = Request.Files;
lblFileList.Text =选择+ hfc1.Count + file(s);
if( hfc1.Count< = 10)// 10 FILES RESTRICTION。
{
for(int i = 0; i< = hfc1.Count - 1; i ++)
{
HttpPostedFile hpf = hfc1 [i];
if(hpf.ContentLength> 0)
{
if(!File.Exists(Server.MapPath(images \\)+
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
新的DirectoryInfo(Server.MapPath(images \\));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
//检查对于复制文件。
FileInfo [] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt,)+。*);
if(objFI.Length> 0)
{
//检查具有相同名称的文件是否支持
foreach(FileInfo文件中) objFI)
{
string sFileName1 = objFI [0] .Name;
string sFileExt1 = Path.GetExtension(objFI [0] .Name );
if(sFileName1.Replace(sFileExt1,)==
sFileName.Replace(sFileExt,))
{
iFailedCnt1 + = 1; //不允许重复。
休息;
}
}
}
else
{
//在文件夹中保存文件。
hpf.SaveAs(Server.MapPath(images \\ )+
Path.GetFileName(hpf.FileName));
iUploadedCnt1 + = 1;
}
}
}
}
lblUploadStatus1.Text =+ iUploadedCnt1 + file已上传。;;
lblFailedStatus1.Text =+ iFailedCnt1 +
重复的文件无法上传。;
}
else lblUploadStatus1.Text =Max。允许10个文件。;
}
else lblUploadStatus1.Text =没有选择文件。;
}
protected void Upload_Files(object sender,EventArgs e)
{
Fileupload();
Fileupload1();
}
('#btUpload')。click(function(){
if(fileUpload.value.length == 0){//检查是否选择了文件。
alert('未选择文件。');
返回false;
}
});
< / script>
< script>
('#btUpload')。 click(function(){
if(fileUpload1.value.length == 0){// CHECK IF FILE(S)SELECTED。
alert('未选择文件。');
返回false;
}
});
< / script>
< / head>
< body>
< form id =form1runat =server>
< asp:FileUpload ID =fileUploadmultiple =truerunat =server/>
< asp:Button ID =btUploadText =上传文件
OnClick =Upload_Filesrunat =server/>
< asp:label id =lblFileListrunat =server>
< asp:标签ID =lblUploadStatusrunat =server>
< asp:Label ID =lblFailedStatusrunat =server>
< asp:FileUpload ID =fileUpload1multiple =truerunat =server/>
< asp:lab el id =lblFileList1runat =server>
< asp:Label ID =lblUploadStatus1runat =server>
< asp:Label ID =lblFailedStatus1runat =server>
< / form>
< / body>
< / html>
.cs代码:
protected void Fileupload()
{
if(fileUpload.HasFile)//检查是否已选择任何文件。
{
int iUploadedCnt = 0;
int iFailedCnt = 0;
HttpFileCollection hfc = Request.Files;
lblFileList.Text =选择+ hfc.Count + file(s);
if(hfc.Count < = 10)// 10 FILES RESTRICTION。
{
for(int i = 0; i< = hfc.Count - 1; i ++)>
{
HttpPostedFile hpf = hfc [i];
if(hpf.ContentLength> 0)
{
if(!File.Exists(Server.MapPath(images \\)) +
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
new DirectoryInfo( Server.MapPath(images \\));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
//检查重复文件。
FileInfo [] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt,)+。*);
if(objFI.Length> 0)
{
//检查具有相同名称的文件是否支持
foreach(FileInfo文件中) objFI)
{
string sFileName1 = objFI [0] .Name;
string sFileExt1 = Path.GetExtension(objFI [0] .Name );
if(sFileName1.Replace(sFileExt1,)==
sFileName.Replace(sFileExt,))
{
iFailedCnt + = 1; //不允许重复。
休息;
}
}
}
else
{
//在文件夹中保存文件。
hpf.SaveAs(Server.MapPath(images \\ )+
Path.GetFileName(hpf.FileName));
iUploadedCnt + = 1;
}
}
}
}
lblUploadStatus.Text =+ iUploadedCnt + file(s)已上传。;
lblFailedStatus.Text =+ iFailedCnt +
无法上传重复的文件。;
}
else lblUploadStatus.Text =Max。允许10个文件。;
}
else lblUploadStatus.Text =没有选择文件。;
}
protected void Fileupload1()
{
if(fileUpload1.HasFile)//检查是否已选择任何文件。
{
int iUploadedCnt1 = 0;
int iFailedCnt1 = 0;
HttpFileCollection hfc1 = Request.Files;
lblFileList.Text =选择+ hfc1.Count + file(s);
if( hfc1.Count< = 10)// 10 FILES RESTRICTION。
{
for(int i = 0; i< = hfc1.Count - 1; i ++)
{
HttpPostedFile hpf = hfc1 [i];
if(hpf.ContentLength> 0)
{
if(!File.Exists(Server.MapPath(images \\)+
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
新的DirectoryInfo(Server.MapPath(images \\));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
//检查对于复制文件。
FileInfo [] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt,)+。*);
if(objFI.Length> 0)
{
//检查具有相同名称的文件是否支持
foreach(FileInfo文件中) objFI)
{
string sFileName1 = objFI [0] .Name;
string sFileExt1 = Path.GetExtension(objFI [0] .Name );
if(sFileName1.Replace(sFileExt1,)==
sFileName.Replace(sFileExt,))
{
iFailedCnt1 + = 1; //不允许重复。
休息;
}
}
}
else
{
//在文件夹中保存文件。
hpf.SaveAs(Server.MapPath(images \\ )+
Path.GetFileName(hpf.FileName));
iUploadedCnt1 + = 1;
}
}
}
}
lblUploadStatus1.Text =+ iUploadedCnt1 + file已上传。;;
lblFailedStatus1.Text =+ iFailedCnt1 +
重复的文件无法上传。;
}
else lblUploadStatus1.Text =Max。允许10个文件。;
}
else lblUploadStatus1.Text =没有选择文件。;
}
protected void Upload_Files(object sender,EventArgs e)
{
Fileupload();
Fileupload1();
}
试试这个
< pre lang =C#> protected void Fileupload()
{
< span class =code-keyword> if (fileUpload.HasFile) // 如果有任何文件,请检查BEEN SELECTED。
{
List< HttpPostedFile> files = new List< HttpPostedFile>();
dynamic fileUploadControlDynamic = fileUpload;
foreach (HttpPostedFile文件 in fileUploadControlDynamic.PostedFiles)
{
files.Add(file) ;
}
int countInControl2 = files.Count;
使用HttpPostedFile
你可以做任何对象..
I want to save all file paths in Database respective of fileupload controllers.I mean When i select 3 files from fileupload1..these files are stored in one table as record with file path,id.Same like that fileupload2 files stored in another form with Path,id.I am usinf 7 File upload controllers in single button click.
What I have tried:
.Aspx code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery Multiple File Upload</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style type="text/css">
#divFile p {
font:13px tahoma, arial;
}
#divFile h3 {
font:16px arial, tahoma;
font-weight:bold;
}
</style>
<script>
$('#btUpload').click(function() {
if (fileUpload.value.length == 0) { // CHECK IF FILE(S) SELECTED.
alert('No files selected.');
return false;
}
});
</script>
<script>
$('#btUpload').click(function () {
if (fileUpload1.value.length == 0) { // CHECK IF FILE(S) SELECTED.
alert('No files selected.');
return false;
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="fileUpload" multiple="true" runat="server" />
<asp:Button ID="btUpload" Text ="Upload Files"
OnClick="Upload_Files" runat="server" />
<asp:label id="lblFileList" runat="server">
<asp:Label ID="lblUploadStatus" runat="server">
<asp:Label ID="lblFailedStatus" runat="server">
<asp:FileUpload ID="fileUpload1" multiple="true" runat="server" />
<asp:label id="lblFileList1" runat="server">
<asp:Label ID="lblUploadStatus1" runat="server">
<asp:Label ID="lblFailedStatus1" runat="server">
</form>
</body>
</html>
.cs code:
protected void Fileupload()
{
if (fileUpload.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED.
{
int iUploadedCnt = 0;
int iFailedCnt = 0;
HttpFileCollection hfc = Request.Files;
lblFileList.Text = "Select " + hfc.Count + " file(s)";
if (hfc.Count <= 10) // 10 FILES RESTRICTION.
{
for (int i = 0; i <= hfc.Count - 1; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
if (!File.Exists(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
new DirectoryInfo(Server.MapPath("images\\"));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
// CHECK FOR DUPLICATE FILES.
FileInfo[] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");
if (objFI.Length > 0)
{
// CHECK IF FILE WITH THE SAME NAME EXISTS
foreach (FileInfo file in objFI)
{
string sFileName1 = objFI[0].Name;
string sFileExt1 = Path.GetExtension(objFI[0].Name);
if (sFileName1.Replace(sFileExt1, "") ==
sFileName.Replace(sFileExt, ""))
{
iFailedCnt += 1; // NOT ALLOWING DUPLICATE.
break;
}
}
}
else
{
// SAVE THE FILE IN A FOLDER.
hpf.SaveAs(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName));
iUploadedCnt += 1;
}
}
}
}
lblUploadStatus.Text = "" + iUploadedCnt + " file(s) Uploaded.";
lblFailedStatus.Text = "" + iFailedCnt +
" duplicate file(s) could not be uploaded.";
}
else lblUploadStatus.Text = "Max. 10 files allowed.";
}
else lblUploadStatus.Text = "No files selected.";
}
protected void Fileupload1()
{
if (fileUpload1.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED.
{
int iUploadedCnt1 = 0;
int iFailedCnt1 = 0;
HttpFileCollection hfc1 = Request.Files;
lblFileList.Text = "Select " + hfc1.Count + " file(s)";
if (hfc1.Count <= 10) // 10 FILES RESTRICTION.
{
for (int i = 0; i <= hfc1.Count - 1; i++)
{
HttpPostedFile hpf = hfc1[i];
if (hpf.ContentLength > 0)
{
if (!File.Exists(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
new DirectoryInfo(Server.MapPath("images\\"));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
// CHECK FOR DUPLICATE FILES.
FileInfo[] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");
if (objFI.Length > 0)
{
// CHECK IF FILE WITH THE SAME NAME EXISTS
foreach (FileInfo file in objFI)
{
string sFileName1 = objFI[0].Name;
string sFileExt1 = Path.GetExtension(objFI[0].Name);
if (sFileName1.Replace(sFileExt1, "") ==
sFileName.Replace(sFileExt, ""))
{
iFailedCnt1 += 1; // NOT ALLOWING DUPLICATE.
break;
}
}
}
else
{
// SAVE THE FILE IN A FOLDER.
hpf.SaveAs(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName));
iUploadedCnt1 += 1;
}
}
}
}
lblUploadStatus1.Text = "" + iUploadedCnt1 + " file(s) Uploaded.";
lblFailedStatus1.Text = "" + iFailedCnt1 +
" duplicate file(s) could not be uploaded.";
}
else lblUploadStatus1.Text = "Max. 10 files allowed.";
}
else lblUploadStatus1.Text = "No files selected.";
}
protected void Upload_Files(object sender, EventArgs e)
{
Fileupload();
Fileupload1();
}
('#btUpload').click(function() {
if (fileUpload.value.length == 0) { // CHECK IF FILE(S) SELECTED.
alert('No files selected.');
return false;
}
});
</script>
<script>
('#btUpload').click(function () {
if (fileUpload1.value.length == 0) { // CHECK IF FILE(S) SELECTED.
alert('No files selected.');
return false;
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="fileUpload" multiple="true" runat="server" />
<asp:Button ID="btUpload" Text ="Upload Files"
OnClick="Upload_Files" runat="server" />
<asp:label id="lblFileList" runat="server">
<asp:Label ID="lblUploadStatus" runat="server">
<asp:Label ID="lblFailedStatus" runat="server">
<asp:FileUpload ID="fileUpload1" multiple="true" runat="server" />
<asp:label id="lblFileList1" runat="server">
<asp:Label ID="lblUploadStatus1" runat="server">
<asp:Label ID="lblFailedStatus1" runat="server">
</form>
</body>
</html>
.cs code:
protected void Fileupload()
{
if (fileUpload.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED.
{
int iUploadedCnt = 0;
int iFailedCnt = 0;
HttpFileCollection hfc = Request.Files;
lblFileList.Text = "Select " + hfc.Count + " file(s)";
if (hfc.Count <= 10) // 10 FILES RESTRICTION.
{
for (int i = 0; i <= hfc.Count - 1; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
if (!File.Exists(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
new DirectoryInfo(Server.MapPath("images\\"));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
// CHECK FOR DUPLICATE FILES.
FileInfo[] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");
if (objFI.Length > 0)
{
// CHECK IF FILE WITH THE SAME NAME EXISTS
foreach (FileInfo file in objFI)
{
string sFileName1 = objFI[0].Name;
string sFileExt1 = Path.GetExtension(objFI[0].Name);
if (sFileName1.Replace(sFileExt1, "") ==
sFileName.Replace(sFileExt, ""))
{
iFailedCnt += 1; // NOT ALLOWING DUPLICATE.
break;
}
}
}
else
{
// SAVE THE FILE IN A FOLDER.
hpf.SaveAs(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName));
iUploadedCnt += 1;
}
}
}
}
lblUploadStatus.Text = "" + iUploadedCnt + " file(s) Uploaded.";
lblFailedStatus.Text = "" + iFailedCnt +
" duplicate file(s) could not be uploaded.";
}
else lblUploadStatus.Text = "Max. 10 files allowed.";
}
else lblUploadStatus.Text = "No files selected.";
}
protected void Fileupload1()
{
if (fileUpload1.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED.
{
int iUploadedCnt1 = 0;
int iFailedCnt1 = 0;
HttpFileCollection hfc1 = Request.Files;
lblFileList.Text = "Select " + hfc1.Count + " file(s)";
if (hfc1.Count <= 10) // 10 FILES RESTRICTION.
{
for (int i = 0; i <= hfc1.Count - 1; i++)
{
HttpPostedFile hpf = hfc1[i];
if (hpf.ContentLength > 0)
{
if (!File.Exists(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir =
new DirectoryInfo(Server.MapPath("images\\"));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
// CHECK FOR DUPLICATE FILES.
FileInfo[] objFI =
objDir.GetFiles(sFileName.Replace(sFileExt, "") + ".*");
if (objFI.Length > 0)
{
// CHECK IF FILE WITH THE SAME NAME EXISTS
foreach (FileInfo file in objFI)
{
string sFileName1 = objFI[0].Name;
string sFileExt1 = Path.GetExtension(objFI[0].Name);
if (sFileName1.Replace(sFileExt1, "") ==
sFileName.Replace(sFileExt, ""))
{
iFailedCnt1 += 1; // NOT ALLOWING DUPLICATE.
break;
}
}
}
else
{
// SAVE THE FILE IN A FOLDER.
hpf.SaveAs(Server.MapPath("images\\") +
Path.GetFileName(hpf.FileName));
iUploadedCnt1 += 1;
}
}
}
}
lblUploadStatus1.Text = "" + iUploadedCnt1 + " file(s) Uploaded.";
lblFailedStatus1.Text = "" + iFailedCnt1 +
" duplicate file(s) could not be uploaded.";
}
else lblUploadStatus1.Text = "Max. 10 files allowed.";
}
else lblUploadStatus1.Text = "No files selected.";
}
protected void Upload_Files(object sender, EventArgs e)
{
Fileupload();
Fileupload1();
}
try this
protected void Fileupload() { if (fileUpload.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED. { List< HttpPostedFile> files = new List<HttpPostedFile>( ); dynamic fileUploadControlDynamic = fileUpload; foreach (HttpPostedFile file in fileUploadControlDynamic.PostedFiles) { files.Add(file); } int countInControl2 = files.Count;
usingHttpPostedFile
object you can do any..
这篇关于如何在ASP.NET中使用具有多个文件上载控制器(如fileupload1,fileupload2,fileupload3)的多个文件上载(任何文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!