没有上传到两个不同的地方 [英] not uploading to two different places

查看:104
本文介绍了没有上传到两个不同的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个页面上有两个输入用于上传文件

输入1:

I have two inputs on a single page for uploading files

Input 1:

<div id="fileUploadarea">											<asp:FileUpload ID="fuPuzzleImage" runat="server" CssClass="fileUpload" /></div>
										<script type="text/javascript">
											function AddMoreImages() {
												if (!document.getElementById && !document.createElement)
													return false;
												var fileUploadarea = document.getElementById("fileUploadarea");
												if (!fileUploadarea)
													return false;
												var newLine = document.createElement("br");
												fileUploadarea.appendChild(newLine);
												var newFile = document.createElement("input");
												newFile.type = "file";
												newFile.setAttribute("class", "fileUpload");
												if (!AddMoreImages.lastAssignedId)
													AddMoreImages.lastAssignedId = 100;
												newFile.setAttribute("id", "FileUpload" + AddMoreImages.lastAssignedId);
												newFile.setAttribute("name", "FileUpload" + AddMoreImages.lastAssignedId);
												var div = document.createElement("div");
												div.appendChild(newFile);
												div.setAttribute("id", "div" + AddMoreImages.lastAssignedId);
												fileUploadarea.appendChild(div);
												AddMoreImages.lastAssignedId++;
											}
										</script>



输入2:




Input 2:


<div id="fileuploadtoitem">
											<asp:FileUpload ID="fuPuzzleImage2" runat="server" CssClass="fileUpload2" />
										</div>
										<script type="text/javascript">
											function AddMoreImages2() {
												if (!document.getElementById && !document.createElement)
													return false;
												var fileuploadtoitem = document.getElementById("fileuploadtoitem");
												if (!fileuploadtoitem)
													return false;
												var newLine2 = document.createElement("br");
												fileuploadtoitem.appendChild(newLine2);
												var newFile2 = document.createElement("input");
												newFile2.type = "file";
												newFile2.setAttribute("class", "fileUpload2");
												if (!AddMoreImages2.lastAssignedId)
													AddMoreImages2.lastAssignedId = 100;
												newFile2.setAttribute("id", "FileUpload" + AddMoreImages2.lastAssignedId);
												newFile2.setAttribute("name", "FileUpload" + AddMoreImages2.lastAssignedId);
												var div2 = document.createElement("div");
												div2.appendChild(newFile2);
												div2.setAttribute("id", "div" + AddMoreImages2.lastAssignedId);
												fileuploadtoitem.appendChild(div2);
												AddMoreImages2.lastAssignedId++;
											}
											</script>



而且我正在尝试将它们上传到两个不同的地方.

输入1:



And i am trying to upload them in two different places.

Input 1 :

HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
    HttpPostedFile hpf = hfc[i];
    if (hpf.ContentLength > 0)
    {
        hpf.SaveAs(Server.MapPath("~/uploads/") +System.IO.Path.GetFileName(hpf.FileName));
    }
    var filesList = new List<string>(hfc.Count);
    string reffilesnames = @"\uploads\" + System.IO.Path.GetFileName(hpf.FileName);
    ref1.Items.Add(reffilesnames);

}



输入2:




Input 2:


HttpFileCollection hfcc = Request.Files;
for (int s = 0; s < hfcc.Count; s++)
{
    HttpPostedFile hpff = hfcc[s];
    if (hpff.ContentLength > 0)
    {
        hpff.SaveAs(Server.MapPath("~/upload2/") + System.IO.Path.GetFileName(hpff.FileName));
    }
    var filesList = new List<string>(hfcc.Count);
    string ref1filesnames = @"\upload2\" + System.IO.Path.GetFileName(hpff.FileName);
    ref2.Items.Add(ref1filesnames);

}



但是最终总是将其上传到两个地方.

知道为什么吗?

这是列表框的屏幕快照



But it always ends up uploading it to both places.

Any idea why?

here is screenshot of listbox

推荐答案

找到了解决方案:

Found the solution:

    protected void Upload(object sender, EventArgs e)
    {
        for (int c = 0; c < Request.Files.Count; c++)
        {
            //var listOfFiles = new List<string>(hfc1.Count);
            HttpPostedFile postedFile = Request.Files[c];
            string uploadFolderName = Request.Files.AllKeys[c].Contains("puzzleImageArea") ? "~/uploads/test1/" : "~/uploads/test2/";
            if (postedFile.ContentLength > 0)
            {
                string fileName = Path.GetFileName(postedFile.FileName);
                postedFile.SaveAs(Server.MapPath(uploadFolderName) + fileName);
                lblMessage.Text += string.Format("{0} uploaded.<br />", fileName);
            }
        }
    }
</string>


这篇关于没有上传到两个不同的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆