簡體   English   中英

單個文件上傳asp.net C#

[英]indiviual files upload asp.net c#

當我單擊鏈接按鈕以添加更多文件上傳時,則僅添加1 ..表示1個文件上傳已可見,而1單擊鏈接按鈕時顯示..

但是當我再次單擊鏈接按鈕以添加更多內容時,則不顯示..

我嘗試這個

好吧,我這樣做

<form id="form1" runat="server">

    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">upload</asp:LinkButton><br />
    <asp:Panel ID="Panel1" runat="server"></asp:Panel>
    <asp:Button ID="Button1" runat="server" Text="submit"  OnClick="Button1_Click" />
 </form>

假設有文件上傳控制..當我們通過單擊瀏覽選擇文件,然后單擊上傳,然后顯示擴展名即abc.doc的文件名時...然后,當我們再次使用相同文件上傳並單擊瀏覽並選擇時然后將顯示另一個文件,即xyz.pdf ..,所以有兩個文件abc.doc和xyz.doc ..所以這是我們想做的..我想上傳文件並顯示這些上傳的文件

更新

為此,我嘗試這個

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        fileuploadd(FileUpload1.FileName);

    }

    public void fileuploadd(string filename)
    {
        try
        {
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {

                    SMSEntities s = new SMSEntities();
                    uploaded_file u = new uploaded_file();
                    {
                        u.fileupload = filename;


                    }
                    s.uploaded_file.Add(u);
                    s.SaveChanges();
                }
                //hpf.SaveAs(Server.MapPath("upload") + "\\" + System.IO.Path.GetFileName(hpf.FileName));
                Response.Write("<b>File: </b>" + hpf.FileName + " <b>Size:</b> " + hpf.ContentLength + " <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>");


            }

        }
        catch (DbEntityValidationException e)
        {
            foreach (var eve in e.EntityValidationErrors)
            {
                Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                       ve.PropertyName, ve.ErrorMessage);
               }
            }
            throw;
        }

    }

因此,當我選擇文件並單擊上載時,文件名將顯示..但是當我再次選擇文件並單擊上載時,第一個文件名將消失,第二個文件名將顯示在我想要的兩個位置。

所以我如何顯示兩個文件名...,然后單擊提交,然后將記錄文件分別插入表中,我這樣做了,但一次都沒有兩個文件名顯示。

在您的.aspx上創建FileUpload控件,例如:

<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />

然后在您的代碼后面:

protected void uploadFile_Click(object sender, EventArgs e)
{
   if (UploadImages.HasFiles)
   {
       foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
       {
           uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"),
           uploadedFile.FileName)); listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
       }
   }
} 

多文件上傳

HI超級用戶以前我也遇到過同樣的問題。 我實際上所做的是,我創建了5個文件控件,例如1)FileUpload1 2)FileUpload2 3)FileUpload3 4)FileUpload4 5)FileUpload5而且我有4個在加載時處於隱藏狀態。 在單擊第一時,我顯示第二個,在單擊第二時,我顯示第三個,依此類推...

在控制器/方法中,我正在檢查文件控件是否不為null,然后通過獲取文件上傳數組來進行正常的文件上傳過程。

     <div id="flOther" runat="server">
    <div id="fileOtherUploadarea">
    <asp:FileUpload ID="flOtherUPL" runat="server" />
    </div>
     <input style="width: 20px; border: 0px none; background-color:transparent;" id="btnOtherAddMoreFiles" type="button"     onclick="AddOtherMoreImages();" class="icon-plus-sign" />|
   <asp:LinkButton ID="lnkOtherUpload" OnCommand="btnLnk_UploadOtherFiles" CommandArgument='<%# Eval("ID")   %>   'runat="server">Upload Files</asp:LinkButton>
    </div>

    <script language="javascript" 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>




protected void btnLnk_UploadOtherFiles(object sender, CommandEventArgs e)
    {

    int ID;



    try
    {
        if (!String.IsNullOrEmpty(Convert.ToString(e.CommandArgument)))
        {
            ID = Convert.ToInt32(e.CommandArgument);

            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    string fileExtension = System.IO.Path.GetExtension(hpf.FileName);
                    if (ValidExtesion(fileExtension))
                    {
                        int MaxSizeAllowed = Convert.ToInt32(ConfigurationManager.AppSettings["MaxFileSize"]);
                        if (hpf.ContentLength < MaxSizeAllowed)
                        {
                            int lastIndex = Convert.ToInt32(hpf.FileName.LastIndexOf("\\"));
                            string FileName = DateTime.Now.Millisecond + hpf.FileName.Substring(lastIndex + 1);
                            string FileName1 = hpf.FileName.Substring(lastIndex + 1);
                            FileName = FileName.Replace(" ", "");

                            StringBuilder AncTag = new StringBuilder();
                            AncTag = AncTag.Append("<a href='Attachments/AMT/'" + FileName + "' target='_blank'>'" + FileName1 + "' </a>");
                            string strAncTag = AncTag.ToString();
                            strAncTag = strAncTag.Replace("'", "");
                            hpf.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "Attachments/AMT/" + FileName);
                            obj.UploadOtherFiles_ProblemDescription(ID, strAncTag);
                        }
                        else
                        {

                        }
                    }
                    else
                    {

                    }
                }
            }


        }
    }
    catch (Exception ex)
    {
        ErrorLog objER = new ErrorLog(ex);
    }
    finally
    {

        obj = null;
        grdPDC.EditIndex = -1;

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM