重新发布相同的问题,因为在发布后,无法编辑问题。如果你之前读过这个问题,请特别阅读底部 -
好的,这是我在这个网站上使用的代码。一切正常,但我需要一些不同的代码上传图像,我不知道该怎么做 - 这里是代码 -
Private Sub btnAttach_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAttach.Click
Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)
If iLength = 0 Then Exit Sub 'not a valid file
Dim sContentType As String = File1.PostedFile.ContentType
Dim sFileName As String, i As Integer
Dim bytContent As Byte()
ReDim bytContent(iLength) 'byte array, set to file size
'strip the path off the filename '
i = InStrRev(File1.PostedFile.FileName.Trim, "\")
If i = 0 Then
sFileName = File1.PostedFile.FileName.Trim
Else
sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i)
End If
Try
File1.PostedFile.InputStream.Read(bytContent, 0, iLength)
With cmdInsertAttachment
.Parameters("@FileName").Value = sFileName
.Parameters("@FileSize").Value = iLength
.Parameters("@FileData").Value = bytContent
.Parameters("@ContentType").Value = sContentType
.ExecuteNonQuery()
End With
Catch ex As Exception
'Handle your database error here
dbConn.Close()
End Try
Response.Redirect(Request.Url.ToString) 'Refresh page
End Sub
一切正常,除非涉及到这一部分 -
With cmdInsertAttachment
.Parameters("@FileName").Value = sFileName
.Parameters("@FileSize").Value = iLength
.Parameters("@FileData").Value = bytContent
.Parameters("@ContentType").Value = sContentType
.ExecuteNonQuery()
End With
我没有使用cmdinsertattachment。我正在使用HTML工具箱中的Html - 输入(文件)。输入文件的ID是ID =“upldimg”。
所以如何将其插入到我的表中 -
Column1 ID identity
Column2 Img image
Column 3 Description varchar(200).
请告诉我插入语句,如 -
INSERT into table1 (Img, Description) values (???, txtdescription.text)
我是否在insert语句中执行upldimg.text?
答案 0 :(得分:0)
如果我这样做,我会使用IIS模块来处理HTTP请求中接收时文件的分块,以避免完全创建临时文件。 Avery简单化的方法看起来像这样:
create table Uploads (
Id int identity(1,1) primary key
, FileName varchar(256)
, ContentType varchar(256)
, FileData varbinary(max));
和aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack && FileUpload1.HasFile)
{
ThreadPool.QueueUserWorkItem(
new WaitCallback(TransferToDatabase), FileUpload1);
}
}
protected void TransferToDatabase(object args)
{
try
{
Debug.Assert(args is FileUpload);
FileUpload upload = (FileUpload)args;
using (SqlConnection conn = new SqlConnection(
Settings.Default.connString))
{
conn.Open();
using (SqlTransaction trn = conn.BeginTransaction())
{
SqlCommand cmd = new SqlCommand(@"
INSERT INTO Uploads(FileName, ContentType, FileData)
VALUES (@FileName, @ContentType, @initialChunk);
SET @id = SCOPE_IDENTITY();", conn, trn);
cmd.Parameters.AddWithValue("@FileName", upload.PostedFile.FileName);
cmd.Parameters.AddWithValue("@contentType", upload.PostedFile.ContentType);
SqlParameter paramId = new SqlParameter("@id", SqlDbType.Int);
paramId.Direction = ParameterDirection.Output;
cmd.Parameters.Add(paramId);
byte[] chunk = new byte[4096];
int offset = upload.FileContent.Read(chunk, 0, 4096);
byte[] initialChunk = chunk;
if (offset < 4096)
{
// can't pass only part of a byte[] as parameter value
// must copy out the appropiate size
initialChunk = new byte[offset];
Array.Copy(chunk, initialChunk, offset);
}
cmd.Parameters.AddWithValue("@initialChunk", initialChunk);
cmd.ExecuteNonQuery();
// Add the rest of the data
if (offset == 4096)
{
SqlParameter paramChunk = new SqlParameter("@chunk", SqlDbType.VarBinary, 4096);
SqlParameter paramLength = new SqlParameter("@length", SqlDbType.BigInt);
SqlCommand cmdAddChunk = new SqlCommand(@"
UPDATE Uploads
SET FileData.Write(@chunk, NULL, @length)
WHERE id = @id", conn, trn);
cmdAddChunk.Parameters.AddWithValue("@id", paramId.Value);
cmdAddChunk.Parameters.Add(paramChunk);
cmdAddChunk.Parameters.Add(paramLength);
do
{
int chunkSize = upload.FileContent.Read(chunk, 0, 4096);
if (0 == chunkSize)
{
break;
}
paramChunk.Value = chunk;
paramLength.Value = chunkSize;
cmdAddChunk.ExecuteNonQuery();
offset += chunkSize;
} while (true);
}
trn.Commit();
}
}
}
catch (Exception e)
{
// Log to the appropiate error logging infrastructure
Debug.Write(e);
}
}