簡體   English   中英

文件上傳器的調用功能

[英]Calling function for file uploader

我想在標簽上隱藏文件上傳控件,我在此鏈接上找到了解決方案:
設置輸入type =“ file”按鈕的樣式
此示例有一個鏈接: http : //jsfiddle.net/4cwpLvae/

現在,通過點擊標簽,它打開的文件上傳並上傳文件后,它會隱藏文件上傳標簽,但我想保存由上傳者在數據庫中上載throuh在函數文件aspx.cs文件。
我該如何調用該函數?

此鏈接對我沒有幫助。 如何使用label中的文件上載器從asp.net中的label.text調用函數背后的代碼僅用於樣式設置。

這是我要調用的函數

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (!inputfile.HasFile)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "isActive", "Test();", true);
            //Response.Write("No file Selected"); return;
        }
        else
        {
            string filename = Path.GetFileName(inputfile.PostedFile.FileName);
            string extension = Path.GetExtension(filename);
            string contentType = inputfile.PostedFile.ContentType;

            HttpPostedFile file = inputfile.PostedFile;
            byte[] document = new byte[file.ContentLength];
            file.InputStream.Read(document, 0, file.ContentLength);
            /*  Stream fs = inputfile.PostedFile.InputStream;
              BinaryReader br = new BinaryReader(fs);
              Byte[] bytes = br.ReadBytes((Int32)fs.Length);*/
            if ((extension == ".pdf") || (extension == ".doc") || (extension == ".docx") || (extension == ".xls")
                || (extension == ".pptx"))//extension  
            {
                if (file.ContentLength <= 31457280)//size  
                {
                    FYPEntities2 obj = new FYPEntities2();
                    tblFile us = new tblFile();
                    us.Name = filename;
                    us.ContentType = contentType;
                    us.Data = document;
                    //  us.Data = bytes;
                    us.Date = DateTime.Now;
                    obj.tblFiles.Add(us);
                    ClientScript.RegisterStartupScript(GetType(), "hwa", "alert('Hello World');", true);
                    obj.SaveChanges();
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
                else
                {

                    ScriptManager.RegisterStartupScript(this, this.GetType(), "isActive", "filesize();", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "isActive", "invalidformat();", true);
            }
        }
    }

我想您正在尋找這個。 將LinkBut​​ton放在頁面上,不給它文本,因此用戶看不到它,但是它仍然存在。

<style>
    input[type="file"] {
        display: none;
    }

    .custom-file-upload {
        border: 1px solid #ccc;
        display: inline-block;
        padding: 6px 12px;
        cursor: pointer;
    }
</style>

<label for="<%=FileUpload1.ClientID %>" class="custom-file-upload">
    <i class="fa fa-cloud-upload">Custom Upload</i>
</label>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="Button1_Click"></asp:LinkButton>

然后,在后面的代碼中,使用LinkButton1UniqueIDonchange屬性附加到FileUpload1 當上傳更改時,javascript將歸檔LinkBut​​ton的PostBack事件,從而自動上傳文件。

protected void Page_Load(object sender, EventArgs e)
{
    FileUpload1.Attributes.Add("onchange", "__doPostBack('" + LinkButton1.UniqueID + "','')");
}

暫無
暫無

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

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