在数据库中存储图像 URL 时,System.Web.HttpInputStream 而不是正确的 sharepoint

本文关键字:HttpInputStream sharepoint Web System 存储 数据库 图像 URL | 更新日期: 2023-09-27 18:30:31

工作解决方案。谢谢大家。希望这有帮助。这涉及获取要存储在数据库列中的 sharepoint 绝对 URL,以及将图片存储在 SharePoint 图片库中

谢谢尼古拉斯

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 = "";
                    }
                }
            }
        }
    }
}

在数据库中存储图像 URL 时,System.Web.HttpInputStream 而不是正确的 sharepoint

如果您希望数据库中

的 SharePoint URL,则应首先将其添加到列表中,就像您正在做的那样,然后您可以获取要放入数据库的 URL。您可以执行以下操作:

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

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