我使用asp.net mvc创建了一个图像上传器,将图像发布到数据库中。但我想添加输入以将相关的URL发送到SQL Server DB。 对于视图部分,我定义了两个输入,如下所示:
<div class="form-group">
<label>Choose File:</label>
<div class="input-group">
<div class="custom-file">
<input type="file" id="fileupload" name="fileupload" class="custom-file-input" /><br />
<input type="text" id="urlname" name="urlname" class="form-control" />
<label class="custom-file-label"></label>
</div>
<div class="input-group-append">
<input type="submit" id="btnUpload" class="btn btn-secondary" value="Upload" />
</div>
</div>
</div>
并且对于控制器,我创建了一个post命令来将数据发送到数据库中。但是我不知道在URL Path变量中放置什么。
[HttpPost]
public ActionResult UploadSliderImage(HttpPostedFileBase fileupload)
{
if (fileupload != null)
{
string fileName = Path.GetFileName(fileupload.FileName);
int fileSize = fileupload.ContentLength;
int Size = fileSize / 1000;
fileupload.SaveAs(Server.MapPath("~/SliderImages/" + fileName));
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spAddNewSliderImage", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@FileSize", Size);
cmd.Parameters.AddWithValue("FilePath", "~/SliderImages/" + fileName);
cmd.Parameters.AddWithValue("@URLPath", ??);
cmd.ExecuteNonQuery();
}
}
return RedirectToAction("UploadSliderImage");
}
这是我创建的用于存储数据的表和过程:
CREATE TABLE [dbo].[CarouselSlider](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[FileSize] [int] NULL,
[FilePath] [nvarchar](100) NULL,
[URLPath] [varchar](100) NULL,
CONSTRAINT [PK_CarouselSlider] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE procedure [dbo].[spAddNewSliderImage]
(
@Name nvarchar(50),
@FileSize int,
@FilePath nvarchar(100),
@URLPath varchar(100)
)
as
begin
insert into CarouselSlider(Name,FileSize,FilePath,URLPath)
values (@Name,@FileSize,@FilePath,@URLPath)
end
CREATE procedure [dbo].[spGetAllSliderImage]
as
begin
Select ID,Name,FileSize,FilePath,URLPath from CarouselSlider
end
谢谢...
答案 0 :(得分:0)
我找到了答案,就在这里:
[HttpPost]
public ActionResult UploadSliderImage(string urlname)
{
string URL = urlname;
....
cmd.Parameters.AddWithValue("@URLPath", URL);
}