SPFile上传文件到文档库

protected   void  UpLoadDocument()
        
{
            
string filename = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("/")+1);
            Response.Write(
"filename:"+filename+"<br/>");
            Stream filedataStream 
= FileUpload1.PostedFile.InputStream;
            
int dataLen = FileUpload1.PostedFile.ContentLength;
            Response.Write(
"dataLen:" + dataLen + "<br/>");
            
string fileType = FileUpload1.PostedFile.ContentType;
            Response.Write(
"fileType:" + fileType + "<br/>");
            
byte[] fileData = new byte[dataLen];
            filedataStream.Read(fileData, 
0, dataLen);

            SPSite sps 
= SPControl.GetContextSite(Context);
            sps.AllowUnsafeUpdates 
= true;
            SPWeb spw 
= sps.OpenWeb();
            spw.AllowUnsafeUpdates 
= true;
            SPList list 
= spw.Lists["TestDocs"];

            SPFolder folder 
= list.RootFolder;
            
bool ex = false;
            Response.Write(
"folder.Exists:" + folder.Exists + "<br/>");
            
//获取不带路径的纯文件名
            filename = FileUpload1.FileName;
            
if (folder.Exists)
            
{
                
try
                
{
                    ex 
= folder.Files[filename].Exists;
                    Response.Write(
"已有同名文件。");
                    
return;
                }

                
catch
                
{
                    folder.Files.Add(filename, fileData, 
true);
                }

            }

        }

你可能感兴趣的:(spfile)