2016-07-29 14 views
0

イメージをMySQLテーブルにアップロードする作業コードがありますが、うまくいきますが、この1つのファイルのアップロードを複数のファイルのアップロードに変換するにはどうすればいいですか?私はのために必要があることを知っているが、私は正確にどこにする必要があります知りません。C#複数のファイルのアップロード

また
protected void UploadFile(object sender, EventArgs e) 
{ 
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); 
    string contentType = FileUpload1.PostedFile.ContentType; 
    int alerta = Convert.ToInt32(this.alertatxt.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) VALUES (@FileName, @ContentType, @Content, @alerta)"; 
       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); 
        con.Open(); 
        cmd.ExecuteNonQuery(); 
        con.Close(); 
       } 
      } 
     } 
    } 
    Response.Redirect(Request.Url.AbsoluteUri); 
} 

誰かがファイルの種類を確認するために私を助けることができる場合は、以前にアップロードされた入力にロードされ、例えば入力のみ

EDITなど.PNG、.JPGを、認めている:

.NET Framework 3.5を使用しています

答えて

0

FileUpload1.PostedFilesを繰り返し、ファイル拡張子を確認するにはpath.GetExtension(fileName)を使用するか、iisのファイルMIMEタイプをアップロードから制限できます。

ドキュメント:

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.postedfiles.aspx

EDIT:

:4.5 以下の.NETフレームワークのために は、AJAX、その後

var fileCollection = Request.Files; 
for (int i = 0; i < fileCollection.Count; i++) 
{ 
HttpPostedFile upload = fileCollection[i]; 
//Do your stuff 
} 

サーバ側でクライアント側経由でファイルをアップロード

function uploadFiles() 
{ 
     var inputElement = document.getElementById("FileUpload1"); 
     var xhr = new XMLHttpRequest(); 
     xhr.onreadystatechange = function() 
     { 
      if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText) { 
       alert("upload done!"); 
      } 
      else { 
       alert("upload failed!"); 
      } 
     }; 
     xhr.open('POST', "WebForm1.aspx/upload"); 
     xhr.setRequestHeader("Content-type", "multipart/form-data"); 
     xhr.send(inputElement.Files); 
} 

Html:

+0

私はPostedFilesを使用しようとするとエラーが発生します。私は3.5フレームワークで作業しているので、これを私の質問に追加することができます。 –

+0

@ F.Flores https://www.microsoft.com/en-us/download/details.aspx?id=30653 – mxmissile

+0

サーバーは3.5以下でのみ動作します。変更するには何かできません。 –

関連する問題