使用的是标准的ASP.Net FileUpload控件来上传文件。
下面就是具体的代码了:
//取得网站
SPWeb site = SPControl.GetContextSite(Context).AllWebs[tbSite_Name.Text];
//SPPictureLibrary pictureLibrary = (SPPictureLibrary)site.Lists[tbList_Title.Text];
//SPUser user = site.CurrentUser;
//site = SPControl.GetContextSite(Context).AllWebs[tbSite_Name.Text];
//取得文件上传控件中的流,FileUpload1是ASP.Net 2.0中的标准文件上传控件
Stream fStream = FileUpload1.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
//这里只是取一个名字而已
string filename = FileUpload1.PostedFile.FileName;
filename = filename.Substring(filename.LastIndexOf("\\")+2,
filename.Length-filename.LastIndexOf("\\")-2);
filename = DateTime.Now.Year.ToString() + "." +
DateTime.Now.Month.ToString() + "." +
DateTime.Now.Day.ToString() + "-" +
DateTime.Now.Hour.ToString() + "." +
DateTime.Now.Minute.ToString() + "." +
DateTime.Now.Second.ToString() + "." +
DateTime.Now.Millisecond.ToString() + "." + filename;
//这里就是上传文件到图片库了,“tbdestUrl.Text + filename”表示保存图片的位置,contents是文件流
//SPFile spfile = site.Files.Add(tbdestUrl.Text + filename, contents,user,user,DateTime.Now,DateTime.Now);
SPFile spfile = site.Files.Add(tbdestUrl.Text + filename, contents);
//这里是读取文件的Exif信息,用到第三方的一个类
System.Drawing.Bitmap bmp =
new System.Drawing.Bitmap(fStream);
Goheer.EXIF.EXIFextractor er =
new Goheer.EXIF.EXIFextractor(ref bmp, "");
//我的图片的每一项都有加一些自定义的属性,这里就可以把读出Exif信息修改上去
spfile.Item["说明"] = ((er["Equip Make"]==null)?"N/a":er["Equip Make"].ToString()) + " "
+ ((er["Equip Model"] == null) ? "N/a" : er["Equip Model"].ToString());
spfile.Item["标题"] = "cleo测试标题";
spfile.Item.Update();
fStream.Close();
SPWeb site = SPControl.GetContextSite(Context).AllWebs[tbSite_Name.Text];
//SPPictureLibrary pictureLibrary = (SPPictureLibrary)site.Lists[tbList_Title.Text];
//SPUser user = site.CurrentUser;
//site = SPControl.GetContextSite(Context).AllWebs[tbSite_Name.Text];
//取得文件上传控件中的流,FileUpload1是ASP.Net 2.0中的标准文件上传控件
Stream fStream = FileUpload1.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
//这里只是取一个名字而已
string filename = FileUpload1.PostedFile.FileName;
filename = filename.Substring(filename.LastIndexOf("\\")+2,
filename.Length-filename.LastIndexOf("\\")-2);
filename = DateTime.Now.Year.ToString() + "." +
DateTime.Now.Month.ToString() + "." +
DateTime.Now.Day.ToString() + "-" +
DateTime.Now.Hour.ToString() + "." +
DateTime.Now.Minute.ToString() + "." +
DateTime.Now.Second.ToString() + "." +
DateTime.Now.Millisecond.ToString() + "." + filename;
//这里就是上传文件到图片库了,“tbdestUrl.Text + filename”表示保存图片的位置,contents是文件流
//SPFile spfile = site.Files.Add(tbdestUrl.Text + filename, contents,user,user,DateTime.Now,DateTime.Now);
SPFile spfile = site.Files.Add(tbdestUrl.Text + filename, contents);
//这里是读取文件的Exif信息,用到第三方的一个类
System.Drawing.Bitmap bmp =
new System.Drawing.Bitmap(fStream);
Goheer.EXIF.EXIFextractor er =
new Goheer.EXIF.EXIFextractor(ref bmp, "");
//我的图片的每一项都有加一些自定义的属性,这里就可以把读出Exif信息修改上去
spfile.Item["说明"] = ((er["Equip Make"]==null)?"N/a":er["Equip Make"].ToString()) + " "
+ ((er["Equip Model"] == null) ? "N/a" : er["Equip Model"].ToString());
spfile.Item["标题"] = "cleo测试标题";
spfile.Item.Update();
fStream.Close();
把项目源码也附上吧:/Files/cleo/UploadPhoto2wss.rar
(我开发的是用户控件,请使用SmartPart或者KaneBoy的包装器部署)