将文件上传转换为多个文件上传c#

时间:2016-08-26 14:17:30

标签: c# asp.net file-upload

您好我正在尝试使用C#创建多个文件上传,目前我只能上传一个,但我需要上传多个,另一件事是我必须显示该文件的预览(仅图像),我有多个文件上传的代码,但只适用于框架4+,我的应用程序是3.5,我正在考虑使用foreach或类似的东西,但我不确定所以我希望你可以帮助我,这是我用于文件上传的C#方法:

protected void UploadFile(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile && this.alertatxt.Text != "" && this.pietxt.Text != "")
        {
            string extension = System.IO.Path.GetExtension(FileUpload1.FileName);

            if (extension == ".jpg" || extension == ".png" || extension == ".jpeg")
            {
                string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
                string contentType = FileUpload1.PostedFile.ContentType;
                int alerta = Convert.ToInt32(this.alertatxt.Text);
                string pie = this.pietxt.Text;
                using (Stream fs = FileUpload1.PostedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                        using (MySqlConnection con = new MySqlConnection(constr))
                        {
                            string query = "INSERT INTO foto(FileName, ContentType, Content, IdAlerta, PieFoto) VALUES (@FileName, @ContentType, @Content, @alerta, @pie)";
                            using (MySqlCommand cmd = new MySqlCommand(query))
                            {
                                cmd.Connection = con;
                                cmd.Parameters.AddWithValue("@FileName", filename);
                                cmd.Parameters.AddWithValue("@ContentType", contentType);
                                cmd.Parameters.AddWithValue("@Content", bytes);
                                cmd.Parameters.AddWithValue("@alerta", alerta);
                                cmd.Parameters.AddWithValue("@pie", pie);
                                con.Open();
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Uploaded.');", true);
                        }

                    }
                    this.pietxt.Text = "";
                }
            }
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('File must be .jpg . png .jpeg');", true);
            this.pietxt.Text = "";
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Fill all the fields.');", true);
            this.alertatxt.Focus();
            this.pietxt.Text = "";
        }

    }

编辑:图片预览不是很重要但是这里是代码:

<div class="col-md-6 text-center form-group">
                                    <asp:FileUpload ID="FileUpload1" runat="server" onchange="showimagepreview(this)" />
                                </div>
    <div class="col-md-6 text-center form-group">
                                <img id="img" alt="" class="img-responsive" style="width: 300px" />
                            </div>

JS:

<script type="text/javascript">

    function showimagepreview(input) {

        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {

                document.getElementsByTagName("img")[0].setAttribute("src", e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }

</script>

1 个答案:

答案 0 :(得分:1)

由于AllowMultiple属性仅添加到.Net 4.0中的FileUpload控件,这意味着您将不得不自己做一些繁重的工作。

如果你有一个较小的固定最大上传量,例如3,你只需向页面添加3个FileUpload控件并循环Request.Files

如果您有最大限制,最好使用javascript动态添加上传字段,并在页面代码中循环Request.Files属性。

有这样做的例子:

http://www.c-sharpcorner.com/uploadfile/prathore/multiple-file-upload-using-jquery-in-asp-net-3-5/

http://www.aspsnippets.com/Articles/Uploading-Multiple-Files-using-FileUpload-Control-in-ASPNet-20-30-and-35.aspx