-1

工作解决方案。谢谢大家。希望这可以帮助。这涉及获取共享点绝对 URL 以存储在数据库列中,并将图片存储在共享点图片库中

谢谢尼古拉斯

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.ComponentModel;
using Microsoft.SharePoint;

namespace UploadDataImage.UploadImage
{
    public partial class UploadImageUserControl : UserControl
    {
        string absURL = string.Empty;
        SqlConnection con = new SqlConnection("server = SP-DEV-MACHINE; Initial Catalog=The_Performance; Integrated Security=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand command = new SqlCommand("select Project_ID from Test1", con);
            command.ExecuteNonQuery();

            DropDownList1.DataSource = command.ExecuteReader();
            DropDownList1.DataValueField = "Project_ID";
            DropDownList1.DataBind();
            con.Close();
        }

        protected void Btnupload_Click(object sender, EventArgs e)
        {
            con.Open();
            using (SPSite site = new Microsoft.SharePoint.SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    Stream StreamImage = null;
                    if (FileUpload1.HasFile)
                    {
                        StreamImage = FileUpload1.PostedFile.InputStream;
                        SPList pics = web.Lists["picture"];
                        SPListItem li = pics.RootFolder.Files.Add(FileUpload1.FileName, StreamImage).GetListItem();

                        absURL = li.Web.Url +"/"+ li.Url;
                        SqlCommand cmd = new SqlCommand("insert into Newsss values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + absURL + "' )", con);

                        cmd.ExecuteNonQuery();

                        con.Close();
                        Label7.Visible = true;
                        Label7.Text = "success";
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        TextBox3.Text = "";
                        TextBox4.Text = "";
                        TextBox5.Text = "";
                        TextBox6.Text = "";
                        TextBox7.Text = "";
                        TextBox8.Text = "";
                        TextBox9.Text = "";
                        TextBox10.Text = "";
                    }
                }
            }
        }
    }
}
4

1 回答 1

0

如果您想要数据库中的 Sharepoint URL,您应该先将其添加到列表中,就像您所做的那样,然后您可以获得要放入数据库的 URL。您可以执行以下操作:

SPListItem li = pics.RootFolder.Files.Add(FileUpload1.FileName, StreamImage).GetListItem();
string absURL = li["EncodedAbsUrl"].ToString();

absURL这将为您提供可以插入表中的文件的完整路径。

于 2013-09-06T19:20:46.663 回答