2008-10-30 4 views
1

ListViewInsertEventArgs에 대해 e.Values ​​메서드를 참조 할 때 linq 및 listview 컨트롤을 사용하여 이미지를 데이터베이스에 업로드 할 수 있지만 ?대신 e.Values.add (... 대신 ListViewEditEventArgs를 사용하여 이미지를 업로드 할 수 있습니다.

보호 무효 ProjectPhotosList_ItemInserting (개체를 보낸 사람, ListViewInsertEventArgs 전자)

{

다음 ListViewEditEventArgs에서 그러한 방법이 없다, 그래서 동일한 결과를 달성하기 위해 사용할 수있는 여기

내 삽입 코드

FileUpload uplImage = (FileUpload) ProjectPhotosList.InsertItem.FindControl ("uplImage");

레이블 fileuploadlbl = (레이블) ProjectPhotosList.InsertItem.FindControl ("fileuploadlbl");

byte[] img = null; 
    if (uplImage.HasFile || !uplImage.FileName.ToLower().EndsWith(".jpg")) 
    { 
     try 
     { 
      img = new byte[uplImage.PostedFile.ContentLength]; 
      uplImage.PostedFile.InputStream.Read(img, 0, img.Length); 
     } 
     catch 
     { 
      fileuploadlbl.Text = "unable to upload " + uplImage.FileName.ToString(); 
     } 
    } 
    if (img == null) 
    { 
     e.Cancel = true; 
     fileuploadlbl.Text = "Please choose a file to upload"; 
    } 

    try 
    { 
     e.Values.Add("ProjectPhoto", new System.Data.Linq.Binary(img)); 
     fileuploadlbl.Text = "File Upload Successful"; 
    } 
    catch 
    { 
     fileuploadlbl.Text = "File Upload Failed, please try again"; 
    } 
} 

답변

0

좋아요. 그래서 문제가 해결되었습니다. 난 그냥 그것에 대해 다른 방식의 비트를 가야했다 :

이 중요한 코드입니다 :

INT의 mykey = int.Parse (ProjectPhotosList.DataKeys은 [e.ItemIndex] .Value.ToString()) ;

선택한 행의 기본 키 값을 가져 오는 간단한 방법입니다. 나는 pdf를 데이터베이스에 업로드하는 것에 관한 게시물을 발견했고 나머지 코드는 그것에 기초를두기로 결정했다. 그래서 여기에 전체 코드 :

보호 무효 ProjectPhotosList_ItemUpdating (개체를 보낸 사람, ListViewUpdateEventArgs 전자)

{

는 FileUpload의 myFile = (는 FileUpload) ProjectPhotosList.EditItem.FindControl ("uploadImage");

TextBox myCaption = (TextBox)ProjectPhotosList.EditItem.FindControl("ProjectPhotoCaptionTextBox"); 

    int mykey = int.Parse(ProjectPhotosList.DataKeys[e.ItemIndex].Value.ToString()); 

    if (myFile.HasFile) 
    { 

     //Get the posted file 
     Stream fileDataStream = myFile.PostedFile.InputStream; 

     //Get length of file 
     int fileLength = myFile.PostedFile.ContentLength; 

     //Create a byte array with file length 
     byte[] fileData = new byte[fileLength]; 

     //Read the stream into the byte array 
     fileDataStream.Read(fileData, 0, fileLength); 

     //get the file type 
     string fileType = myFile.PostedFile.ContentType; 

     //Open Connection 
     PHJamesDataContext db = new PHJamesDataContext(); 
     //Find the Right Row 
     PHJProjectPhoto Newphoto = (from p in db.PHJProjectPhotos 
            where p.ProjectPhotoId == mykey 
            select p).Single<PHJProjectPhoto>(); 


     Newphoto.ProjectPhoto = fileData; 

     db.SubmitChanges(); 
    } 
관련 문제