Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
HI I have the following:
Response.Write(Request.Headers("X-File-Name"))

Dim input As Stream = DirectCast(Request.InputStream, Stream)
Dim bmp As New Bitmap(input)

Response.Clear()
Response.ContentType = "image/jpeg"

' Save to the Response.OutputStream object
bmp.Save("C:\inetpub\wwwroot\mywerver\upload\" & Request.Headers("X-File-Name") & ".jpg")

bmp.Dispose()

which work fine to upload images but if I want to upload some other extension like PDF, then it wont work.

What I have tried:

I tried : Response.ContentType = "application/pdf" and
bmp.Save("C:\inetpub\wwwroot\mywerver\upload\" & Request.Headers("X-File-Name") & ".pdf")
But no luck.
Thanks for any help
James
Posted
Updated 21-Mar-18 12:58pm
v2
Comments
j snooze 20-Mar-18 17:34pm    
Is this being posted to your page from an upload file control? Also is this webforms or mvc?
jameskm69 21-Mar-18 7:31am    
Hi J, this is not from a asp upload controller. it's from a javascript/AJAX:
if (file) {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', uploadProgress, false);
xhr.onreadystatechange = stateChange;
xhr.open('POST', 'crop1.aspx', true);
xhr.setRequestHeader('X-FILE-NAME', file.name);
xhr.send(file);
}
function stateChange(event) {

if (event.target.readyState == 4) {

if (event.target.status == 200 || event.target.status == 304 || event.target.status == 500 ) {
$('#dropZone').text('Upload Complete!');
count2 = count2 + 1;
if (count2 == count) {
alert("You Uploaded " + count2 + " Photo(s) \n Click ''OK'' to continue");
}
}
else {
dropZone.text('Upload Failed!');
dropZone.addClass('error');
}
}
}
note: crop1.aspx is the page as WebRequest
Thanks,
James
j snooze 21-Mar-18 17:29pm    
This may help you out.
https://stackoverflow.com/questions/2934295/c-sharp-save-a-file-from-a-http-request#2934308
its in c# but you should be able to convert it over to VB

1 solution

This will work with any file type on the server
VB
Response.Write(Request.Headers("X-File-Name"))
            
Dim output As FileStream = File.Create(PathToFolderEndWithBackSlash & Request.Headers("X-File-Name"))
Dim input As Stream = DirectCast(Request.InputStream, Stream)
            
Dim buffer(input.Length - 1) As Byte
Dim bytesRead As Integer
               
Do
   bytesRead = input.Read(buffer, 0, buffer.Length)
   If bytesRead = 0 Then Exit Do
   output.Write(buffer, 0, bytesRead)
Loop

output.Close()
though I dare asy you'll want to add error catching etc etc
 
Share this answer
 
v3
Comments
A_Griffin 22-Mar-18 4:18am    
btw, I updated the Do...Loop to check for the bytesRead = 0 condition before writing - it does actually work the other way (having it in the Until ...) but still ...
jameskm69 22-Mar-18 7:40am    
Perfect, Thanks Griffin. That worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900