PDA

View Full Version : ASP: Add & Delete attachment



apple
08-25-2007, 10:20 PM
Hi,

Can anyone help me to add attachment and delete attachment in ASP.?

Thanks

Bob Phillips
08-26-2007, 01:58 AM
ASP uses VBScript, so it would be very similar to VBA.

Where are you trying to add the attachment?

apple
08-27-2007, 12:36 AM
Hi,

Please refer the attchment. I want to do in frontpage and is web based.

Thanks

mvidas
08-27-2007, 12:55 PM
Hi apple,

To upload files in my asp pages, I use the free component "aspSmartUpload" -- search for it in google, their page has both the download as well as working examples. Pretty easy to use too.

apple
08-27-2007, 11:22 PM
Hi,

Can anybody show me the method to delete attachment from database?

fixo
08-29-2007, 12:02 AM
Hi,

Can anybody show me the method to delete attachment from database?

Not clearly enough what the attacments
you means
See attached file it may helps I hope

~'J'~

apple
08-29-2007, 01:04 AM
Hi,

Thanks. I want do it in ASP. I already done upload attachment but i not sure how to delete attachment from database.

fixo
08-29-2007, 01:30 AM
Sorry, but I have not working with ASP yet

:(

~'J'~

apple
08-29-2007, 01:35 AM
Hi,

I will attach what i done later for more details. Thanks

apple
08-29-2007, 08:07 AM
Hi,

Can anyone help me to delete attachment. I only able to upload file but i have no idea delete file from database.


The interface:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Deletefile</title>
</head>
<body>
<div align=Center>
<form method="post" action="upload.asp" enctype="multipart/form-data" onsubmit="return validate_form(this)">
<table border=1 width="582" algin="center">
<tr>
<td width="139" align="center"><font face="Arial" size=2>Select&nbsp; file

<td width="345" align="center">
<input type="file" name="nDoc" size=35 style="color: #000080; text-decoration: blink" onSubmit="return validateSpace()"></td>

<td align="center" width="72"><input type="submit" value="upload">
<td align="center" width="72"><input type="submit" value="delete">
</tr>
</table>
</div>
</form>


</body>
</html>




Below is the coding for upload


<html>
<head></head>

<body>

<%
'FileName = Session("nDoc")
'if Instr(1,FileName,"xls") > 0 then
%>
<script language="Javascript">
//alert("Please upload .xls format for file")
//history.go(-1)
</script>
<%
''response.redirect "Uploadfile.asp"
'end if
%>

<%

dim fn, temp(2),cc, sql, movie
cc=0

' used to track various positions
dim PosB, PosBBound, PosEBound, PosEHead, PosBFld, PosEFld

' these handle the data
dim Boundary, BBoundary, PartBHeader, PartAHeader, PartContent, PartContent2, Binary

' for writing and converting
dim fso, fle, rst, DataString, FileName

' various other
dim I, Length, ContType, PartName, LastPart, BCrlf, PartContentLength

' ado constants
const adLongVarBinary = 205
const adLongVarchar = 201

' must be submitted using POST
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then

ContType = Request.ServerVariables("HTTP_Content_Type")
' must be "multipart/form-data"
If LCase(Left(ContType, 19)) = "multipart/form-data" Then
PosB = InStr(LCase(ContType), "boundary=") 'get boundary
If PosB > 0 Then Boundary = Mid(ContType, PosB + 9) 'we have one

'bugfix IE5.01 - double header
PosB = InStr(LCase(ContType), "boundary=")
If PosB > 0 then
PosB = InStr(Boundary, ",")
If PosB > 0 Then Boundary = Left(Boundary, PosB - 1)
end if

Length = CLng(Request.ServerVariables("HTTP_Content_Length")) 'Get Content-Length header
End If

If Length > 0 And Boundary <> "" Then
Boundary = "--" & Boundary

' get request, binary
Binary = Request.BinaryRead(Length)

' convert boundry to binary
For I=1 to len(Boundary)
BBoundary = BBoundary & ChrB(Asc(Mid(Boundary,I,1)))
Next

' binary crlf
BCrlf = ChrB(Asc(vbCr)) & ChrB(Asc(vbLf))

' get begin and end of first boundary
PosBBound = InStrB(Binary, BBoundary)
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary, 0)

' keep doing until we had them all
Do While (PosBBound > 0 And PosEBound > 0)


' get position of the end of the header
PosEHead = InStrB(PosBBound + LenB(BBoundary), Binary, BCrlf & BCrlf)

' get content of header and convert to string
PartBHeader = MidB(Binary, PosBBound + LenB(BBoundary) + 2, PosEHead - PosBBound - LenB(BBoundary) - 2)
PartAHeader = ""
For I=1 to lenb(PartBHeader)
PartAHeader = PartAHeader & Chr(AscB(MidB(PartBHeader,I,1)))
Next

' make sure we end it with ;
If Right(PartAHeader,1) <> ";" Then PartAHeader = PartAHeader & ";"

' get content of this part
PartContent = MidB(Binary, PosEHead + 4, PosEBound - (PosEHead + 4) - 2)


' get name of part
PosBFld = Instr(lcase(PartAHeader),"name=")
If PosBFld > 0 Then
' name found
PosEFld = Instr(PosBFld,lcase(PartAHeader),";")
If PosEFld > 0 Then
' well-formed name header
PartName = Mid(PartAHeader,PosBFld+5,PosEFld-PosBFld-5)
end if
' chop of leading and trailing "'s
Do Until Left(PartName,1) <> """"
PartName = Mid(PartName,2)
Loop
Do Until Right(PartName,1) <> """"
PartName = Left(PartName,Len(PartName)-1)
Loop
end if

' get file name of part (if any)
PosBFld = Instr(lcase(PartAHeader),"filename=""")
If PosBFld > 0 Then
' content header found
PosEFld = Instr(PosBFld + 10,lcase(PartAHeader),"""")
If PosEFld > 0 Then
' well-formed content header
FileName = Mid(PartAHeader,PosBFld+10,PosEFld-PosBFld-10)
end if
' chop of leading and trailing "'s
Do Until Left(FileName,1) <> """"
FileName = Mid(FileName,2)
Loop
Do Until Right(FileName,1) <> """"
FileName = Left(FileName,Len(FileName)-1)
Loop
Else
FileName = ""
end if


' do conversion of binary to regular data
' at the end, datastring will contain 'readable' data
' is this wide-byte binary data?
if vartype(PartContent) = 8 then
' need to do some conversion
Set rst = CreateObject("ADODB.Recordset")
PartContentLength = LenB(PartContent)
if PartContentLength > 0 then
' data, so add to recordset to speed up conversion
rst.Fields.Append "data", adLongVarBinary, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent & ChrB(0)
rst.Update
PartContent2 = rst("data").GetChunk(PartContentLength)
rst.close
set rst = nothing
else
' no data?
PartContent2 = ChrB(0)
End If
else
' no need for conversion
PartContent2 = PartContent
end if

PartContentLength = LenB(PartContent2)
if PartContentLength > 0 then
' we have data to convert
Set rst = CreateObject("ADODB.Recordset")
rst.Fields.Append "data", adLongVarChar, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent2
rst.Update
DataString = rst("data")
rst.close
set rst = nothing
Else
' nothing to convert
dataString = ""
End If

' conversion has been done, now what to do with it

If FileName <> "" Then
' we have a file, let's save it to disk

FileName = Mid(Filename,InstrRev(FileName,"\")+1)

' open a file (textstream)
set fso = Server.CreateObject("Scripting.Filesystemobject")
set fle = fso.CreateTextFile(server.MapPath("attachment_file\" & FileName))
' write the data
fle.write DataString
fle.close
' cleanup
set fle = nothing
set fso = nothing

' give notification

'Response.Write "<BR>Uploaded file " & Partname & " - " & FileName & "(" & Len(Datastring) & " bytes)" & "<BR><BR>"
fn = FileName
else
' some other type of field, let's just output this

'Response.Write "<BR>Form field: " & Partname & " - " & Datastring

temp(cc) = Datastring
cc = cc + 1

End If

LastPart = MidB(Binary, PosEBound + LenB(BBoundary), 2)

If LastPart = ChrB(Asc("-")) & ChrB(Asc("-")) Then
' don't look for others
PosBBound = 0
PosEBound = 0
else
' look for others
PosBBound = PosEBound
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary)
End If

loop

end if

end if

%>

<%

if FileName <> "" then
dim newSql
If InStr(1, fn, " ") > 0 Then
%>
<script language="Javascript">
alert("Please No space for your File!")
history.go(-1)
</script>
<%
end if

SET DB=Server.CreateObject("ADODB.Connection")
DB.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
Set RS=Server.CreateObject("ADODB.Recordset")
RS.CursorType = 2
RS.LockType = 3
'sqlstr = "Select * from Temp where Session=" & Session("iden")
''response.write(sqlStr)
'RS.open sqlstr, DB,3
newSql = "Insert into guestbook (path) values ('" &"attachment_file/" &fn &"')"
'response.write(newSql)
DB.Execute newSql


'RS.Close
DB.Close
Set RS=nothing
Set DB=nothing

%>

</script>
<%

else

SET DB=Server.CreateObject("ADODB.Connection")
DB.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbooks.mdb")
Set RS=Server.CreateObject("ADODB.Recordset")
RS.CursorType = 2
RS.LockType = 3
'sqlstr = "Select * from Temp where Session=" & Session("iden")
''response.write(sqlStr)
'RS.open sqlstr, DB,3
newSql = "Insert into guestbook (path) values ('"&"','attachment_file/NO File')
'response.write(newSql)
DB.Execute newSql


'RS.Close
DB.Close
Set RS=nothing
Set DB=nothing



end if

%>

</body>
</html>