c# – 无法将图像上载到SharePoint列表

栏目: ASP.NET · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/13602717/cannot-upload-image-to-a-sharepoint-list
我正在使用Visual WebPart,我想将我的FileUpload控件上的图像上传到SharePoint列表.这是我正在使用的代码,但我无法让它工作,(标题,prodnum,颜色等工作,但不是图像).我也安装了 SparQube .

这是我的ListView:

Image http://i47.tinypic.com/x5z2v7.jpg .

protected void Button_Save_Click(object sender, EventArgs e)
{
    SPSite currentSite = SPContext.Current.Site;
    SPList myList = currentSite.RootWeb.Lists.TryGetList("SharePointDatabase");

    try
    {
        if (myList != null && FileUpload_Pic.PostedFile != null && FileUpload_Pic.HasFile)
        {
            SPListItem listItem = myList.Items.Add();

            listItem["Title"] = TextBox_Name.Text;
            listItem["ProductNumber"] = TextBox_ProdNum.Text;
            listItem["Color"] = TextBox_Color.Text;
            listItem["ListPrice"] = TextBox_ListPrice.Text;
            listItem["MoreInformation"] = TextBox_MoreInfo.Text;

            string fileName = Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
            listItem["Image"] = fileName;

            listItem.Update();                                            

            TextBox_Search.Text = string.Empty;
            TextBox_Name.Text = string.Empty;
            TextBox_MoreInfo.Text = string.Empty;
            TextBox_ProdNum.Text = string.Empty;
            TextBox_Color.Text = string.Empty;
            TextBox_ListPrice.Text = string.Empty;

            Label_Exception.Text = "Saved to Database list.";

            Dispose();
        }
    }
    catch (Exception x)
    {
        Label_Exception.Text = x.Message;
    }

}

您可以使用以下方法将文件直接从文件上载添加到特定Web,然后将文件路径添加到列表中,如下例所示,

SPContext.Current.Web.Files.Add(String.Concat(SPContext.Current.Web.Site.RootWeb.Url, path), stream, true);

path是图像的相对路径.在用户案例文件名中.

stream可以在文件上传控件中使用FileUpload.FileContent

然后将此路径添加到列表中,如下所示.

listItem["Image"] = path;

这适用于所有浏览器.

翻译自:https://stackoverflow.com/questions/13602717/cannot-upload-image-to-a-sharepoint-list


以上所述就是小编给大家介绍的《c# – 无法将图像上载到SharePoint列表》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Weaving the Web

Weaving the Web

Tim Berners-Lee / Harper Paperbacks / 2000-11-01 / USD 15.00

Named one of the greatest minds of the 20th century by Time , Tim Berners-Lee is responsible for one of that century's most important advancements: the world wide web. Now, this low-profile genius-wh......一起来看看 《Weaving the Web》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器