2012-04-23 33 views
0

SharePoint Foundation Server 2010用のシンプルなビジュアルWebPartを開発しています。 イメージファイルをSharePointサーバーにアップロードしてから表示することになっています。 以前に作成したドキュメントライブラリにファイルを正常にアップロードすることはできますが、ファイルを表示することはできません(IEでは赤い十字が表示されます)。 SharePointフロントエンドを使用してファイルの正確なコピーをアップロードすると、そのファイルを開くことができます。誰かが私に何が欠けているか教えてくれることを願っています。アップロードしたSharePointの画像ファイルを表示できません

SPContext.Current.Web.AllowUnsafeUpdates = true; 
     string path = ""; 
     string[] fileName = filePath.PostedFile.FileName.Split('\\'); 
     int length = fileName.Length; 
     // get the name of file from path 
     string file = fileName[length - 1]; 
     SPWeb web = SPContext.Current.Web; 
     SPFolderCollection folders = web.Folders; 
     SPFolder folder; 
     SPListCollection lists = web.Lists; 
     SPDocumentLibrary library; 
     SPList list = null; 
     Guid guid = Guid.Empty; 

     if (lists.Cast<SPList>().Any(l => string.Equals(l.Title, "SPUserAccountDetails-UserImages"))) 
     { 
      list = lists["SPUserAccountDetails-UserImages"]; 
     } 
     else 
     { 
      guid = lists.Add("SPUserAccountDetails-UserImages", "Enthält Mitarbeiter-Fotos", SPListTemplateType.DocumentLibrary); 
      list = web.Lists[guid]; 
     } 

     library = (SPDocumentLibrary)list; 

     folder = library.RootFolder.SubFolders.Add("SPUserAccountDetails"); 

     SPFileCollection files = folder.Files; 
     Stream fStream = filePath.PostedFile.InputStream; 
     byte[] MyData = new byte[fStream.Length]; 
     Stream stream = new MemoryStream(); 
     stream.Read(MyData, 0, (int)fStream.Length); 
     fStream.Close(); 
     bool bolFileAdd = true; 
     for (int i = 0; i < files.Count; i++) 
     { 
      SPFile tempFile = files[i]; 
      if (tempFile.Name == file) 
      { 
       folder.Files.Delete(file); 
       bolFileAdd = true; 
       break; 
      } 
     } 
     if (bolFileAdd) 
     { 
      SPFile f = files.Add(file, MyData); 

      f.Item["ContentTypeId"] = "image/jpeg"; 
      f.Item["Title"] = file; 
      f.Item.SystemUpdate(); 

      SPContext.Current.Web.AllowUnsafeUpdates = false; 
      imgPhoto.ImageUrl = (string)f.Item[SPBuiltInFieldId.EncodedAbsUrl]; 
     } 

答えて

0

気にしない:あなた以下

が正常にサーバーにファイルをアップロードコードを見つけることができます。私のコードは、ファイルの内容を混乱させるようです。後で解決策を投稿します。

編集: 私は愚かだと申し訳ありません: - :これで

Stream fStream = filePath.PostedFile.InputStream; 
byte[] MyData = new byte[fStream.Length]; 
Stream stream = new MemoryStream(); 
stream.Read(MyData, 0, (int)fStream.Length); 
fStream.Close(); 

:私はこの置き換え/

Stream fStream = filePath.PostedFile.InputStream; 
byte[] MyData = new byte[fStream.Length]; 
BinaryReader binaryReader = new BinaryReader(fStream); 
MyData = binaryReader.ReadBytes((Int32)fStream.Length); 
fStream.Close(); 
binaryReader.Close(); 

を、突然それがすべて働きました; - )

関連する問題