调用文件上传器

时间:2016-12-13 16:06:11

标签: javascript c# html asp.net file-upload

我想隐藏标签背后的文件上传控件我在此链接上找到了解决方案:
Styling an input type="file" button
这个例子有一个链接:http://jsfiddle.net/4cwpLvae/

现在点击标签打开文件上传器,上传文件后隐藏文件上传选项卡,但是我想通过aspx.cs文件中的函数保存上传者上传的文件。
我怎么称呼这个功能?

这个链接对我没有帮助 How to call code behind function from label.text in asp.net 在标签中使用文件上传器仅用于样式。

这是我要打电话的功能

 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);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为你正在寻找这个。在页面上放置一个LinkBut​​ton并且不给它Text,因此它对用户不可见但仍然存在。

<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>

然后在代码后面使用onchange FileUpload1UniqueID属性附加到LinkButton1。当上传更改时,javascript将提交LinkBut​​ton的PostBack事件,从而自动上传文件。

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