2016-09-19 12 views
0

私のフォームに複数のファイルアップロードコントロールがあります。私は、添付ファイルの種類を選択するためのドロップダウンを使用して、データベースに保存するファイルの数だけアップロードしようとしています。私はデータベースにファイルを保存することができますが、私はすべて私のデータ(0x0000000000000000000000000000000000000000 ... 00)の0を取得します。 ユーザーがファイルリンクをクリックしたときにファイルを取得しようとすると、syas "pdfドキュメントを読み込めませんでした"というエラーが表示されます。ここでC#でfileuploadControlを使用してデータベースにデータを保存する

は、それをデータベースに保存するための私のコードです:

public void getDynamicFilesFromUploadedFileList(int wWireTransferID, int wUserID) 
    { 
     if (GridView1.Rows.Count > 0) 
     { 
      foreach (GridViewRow grow in GridView1.Rows) 
      { 
       try 
       { 
        FileUpload fileuploadControl = new FileUpload(); 
        fileuploadControl = (FileUpload)grow.FindControl("fUpControl"); 

        DropDownList drpFileType = new DropDownList(); 
        drpFileType = (DropDownList)grow.FindControl("ddlFileType"); 
        Guid newID = Guid.NewGuid(); 

        if (fileuploadControl.HasFile) 
        { 
         //I believe here is my mistake 
         Byte[] fileData = new Byte[fileuploadControl.PostedFile.ContentLength]; 

         new MRSManager().InsertWTDocument(wWireTransferID, 
          fileuploadControl.FileName, 
          drpFileType.SelectedValue.Trim(), 
          fileuploadControl.PostedFile.ContentType, 
          System.IO.Path.GetExtension(fileuploadControl.FileName), 
          fileuploadControl.PostedFile.ContentLength.ToString(), 
          fileData, 
          wUserID, 
          newID); 
        } 
        else 
        { 
         string m = "No File! "; 
        } 
       } 
       catch (Exception Exp) 
       { 
        string msg = Exp.Message.ToString(); 
       } 
      } 
     } 
    } 

enter image description here

そして、これは私がファイルを取得しようとどのように上のコードです。

protected void Page_Load(object sender, EventArgs e) 
    { 
     // Get the file id from the query string 
     string id = Request.QueryString["ID"]; 

     // Get the file from the database 
     DataTable file = GetAFile(id); 
     DataRow row = file.Rows[0]; 

     string name = (string)row["FileName"]; 
     string contentType = (string)row["ContentType"]; 
     Byte[] data = (Byte[])row["Data"]; 

     Response.Clear(); 
     Response.ClearContent(); 
     Response.ClearHeaders(); 
     // Send the file to the browser 
     Response.AddHeader("Content-type", contentType); 
     Response.AddHeader("Content-Disposition", "inline; filename=" + name); 
     Response.Buffer = true; 
     Response.BinaryWrite(data); 
     Response.Flush(); 
     Response.Close(); 
     Response.End(); 
    } 

    // Get a file from the database by ID 
    public static DataTable GetAFile(string id) 
    { 
     DataTable file = new DataTable(); 
     string sSQL = string.Format("SELECT * FROM tblWireTransferDocuments WHERE FileID = '{0}'", id); 
     file = SqlHelper.ExecuteDataset(DBConnection.GetConnectionString(), CommandType.Text, sSQL, null).Tables[0]; 
     return file; 
    } 
} 

}

+0

これを確認してください。http://stackoverflow.com/questions/2579373/saving-any-file-to-in-the-database-just-convert-it-to-a-byte-array – DanielVorph

答えて

2

あなたが唯一のFILEDATAを作成し、それを埋めていなかった右

//I believe here is my mistake 
Byte[] fileData = new Byte[fileuploadControl.PostedFile.ContentLength]; 

です。 fileDataを満たすには、fileuploadControl.FileContentを使用する必要があります。

+0

ストリームとそこから読んでください。 using(System.IO.Stream myStream = fileuploadControl.FileContent) { myStream.Read(fileData、0、fileuploadControl.PostedFile.ContentLength); } – steryd

+0

ありがとうございました – S5498658

関連する問題