We are converting from IIS6 to IIS7 and all has went very well except our download section. What happens is the download is initiated, but it appears a redirect is taking place and our default.aspx page is downloaded instead of the requested file by the user. Below is the code we used with IIS6.
Private Sub GetFile(ByVal ReportQueueId As System.Int32, _
ByVal FileName As System.String _
)
Dim stream As System.IO.MemoryStream = Nothing
Dim lngRecordCount As System.Int32 = 0
Dim objWebClient As New System.Net.WebClient
Dim strServerName As System.String
Try
strServerName = Page.Request.Item("SERVER_NAME").ToString()
Dim fURI As New System.Uri("http://" & strServerName & "/reportmonitor/" & FileName)
' Open the file into a stream.
stream = New System.IO.MemoryStream(objWebClient.DownloadData(fURI), False)
' Total bytes to read:
Dim bytesToRead As Long = stream.Length
Page.Response.Clear()
Page.Response.ContentType = "application/octet-stream"
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" & FileName.ToString())
Page.Response.AddHeader("Content-Length", bytesToRead.ToString())
' Read the bytes from the stream in small portions.
While bytesToRead > 0
' Make sure the client is still connected.
If Response.IsClientConnected Then
' Read the data into the buffer and write into the output stream.
Dim buffer As Byte() = New Byte(9999) {}
Dim length As Integer = stream.Read(buffer, 0, 10000)
Response.OutputStream.Write(buffer, 0, length)
Response.Flush()
' We have already read some bytes.. need to read
' only the remaining.
bytesToRead = bytesToRead - length
Else
' Get out of the loop, if user is not connected anymore..
bytesToRead = -1
End If
End While
'Update status
lngRecordCount = UpdateStatus(ReportQueueId, _
listcounts_common.ListCountsCommon_CL.ReportQueueStatus.rqsDownloaded _
)
Catch SystemException As System.Exception
'Update status
lngRecordCount = UpdateStatus(ReportQueueId, _
listcounts_common.ListCountsCommon_CL.ReportQueueStatus.rqsOnHold _
)
'most likely a 404 file not found error
Me.lblErrorMessage.Text = CLASS_NAME & ":GetFile: " & SystemException.Message.ToString
Me.lblErrorMessage.Visible = True
Finally
objWebClient = Nothing
stream = Nothing
End Try
End Sub
After this code is run the only other thing I have read it might be is in the parent function that calls GetFile() we have code for the following:
' stops page html output. If this is not done, un-desired html code will be added to csv files
Page.Response.End()
Any thoughts on the difference between IIS6 and 7 and this process? Everything I have tried has not worked. The new site is running in Itegrated Mode, .NET 4.
Thank you...
UPDATE
I modified the fURI to be an external file:
fURI = New System.Uri("http://manuals.info.apple.com/en_US/ipad_user_guide.pdf")
This file downloads perfectly, so I am assuming it is a permissions issue in IIS7...any ideas on what I may have missed?
Since the site is using Forms Authentication, adding an allowable location path in the web.config for the virtual resolved the issues in converting to IIS7.
<location path="reportmonitor">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Related
This question already has answers here:
Zip file is getting corrupted after downloading from server in C#
(3 answers)
Closed 4 years ago.
I am trying to allow authenticated users to upload pictures to the server through FTP. The code works for the most part. The part that doesn't is that there is an issue in uploading the file. I have tried to upload a few different pictures and all of them are larger on the server and therefore, not properly constructed.
One picture I tried is 4.56MB on my computer and 8.24MB on the server. When I load the picture in Photo, it states "We can't open this file." The page location is at http://troop7bhac.com/pages/slideshowedit.aspx. The following is the VB.NET code behind the upload:
Sub uploadFile_Click(sender As Object, e As EventArgs)
lblUploadErrors.InnerHtml = ""
If (lstSlideshowChoose.SelectedValue = "") Then
lblUploadErrors.InnerHtml = "<p>A slideshow must be selected.</p>"
Else
If (FileUpload1.HasFile) Then
Dim nameList() As String
Dim successList() As String
Dim i As Integer = 0
For Each file As HttpPostedFile In FileUpload1.PostedFiles
Dim fileBytes() As Byte = Nothing
Dim fileName As String = Path.GetFileName(file.FileName)
Dim photoRE As New Regex("^[A-z0-9 _]{1,}\.jpg|JPG|jpeg|JPEG|png|PNG+$")
Dim photoSuccess As Boolean = photoRE.Match(fileName).Success
ReDim Preserve nameList(i)
ReDim Preserve successList(i)
If (photoSuccess = True) Then
Using fileStream As New StreamReader(file.InputStream)
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
fileStream.Close()
End Using
Try
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(ftpPath & lstSlideshowChoose.SelectedValue & "/" & fileName), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(ftpUser, ftpPass)
Using uploadStream As Stream = request.GetRequestStream()
uploadStream.Write(fileBytes, 0, fileBytes.Length)
uploadStream.Close()
End Using
Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
response.Close()
successList(i) = "Success "
Catch ex As Exception
successList(i) = "Failed "
End Try
Else
successList(i) = "Failed "
End If
nameList(i) = fileName
i += 1
Next
For x As Integer = 0 To nameList.Count - 1
lblUploadErrors.InnerHtml += "<p>" & successList(x) & nameList(x) & "</p>"
Next
Else
lblUploadErrors.InnerHtml = "<p>You have not selected a picture to upload.</p>"
End If
End If
End Sub
The files are obtained through an ASP.NET FileUpload control. The control has been set to allow multiple files at once.
Any help to figure out why the pictures are not uploading properly would be greatly appreciated.
EDIT: I tried Martin Prikryl's possible duplicate solution. Had to change it from C# to VB.NET. It failed. I tried David Sdot's solution and it also failed. Both solutions returned the same errors.
If the page was ran on my local machine, it returned "C:\Program Files (x86)\IIS Express\PictureName.JPG." If the page was ran on the server, it returned "C:\Windows\SysWOW64\inetsrv\PictureName.JPG." Both errors are of the System.IO.FileNotFoundException class.
Your Problem is here:
Using fileStream As New StreamReader(file.InputStream)
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd())
fileStream.Close()
Using
Your image is read as text. From this text you get the bytes UTF8 byte values, thats why your image is nearly twice the size when uplaoded. You need the bytes from the image, without converting them to something else.
fileBytes = File.ReadAllBytes(file.FileName)
I check for file size and file extension already. But I still need to check if the upload process has timed out. If it does I want to display a message to the user that the upload timed out and they can try again later. Any help would be greatly appreciated! :)
Dim success As Boolean = False
Response.Write("*EIL*")
Try
If Not Context.Request.Files Is Nothing Then
Dim fileCount As Int32 = Context.Request.Files.Count
For fileLoop As Integer = 0 To fileCount - 1
Dim file As HttpPostedFile = Context.Request.Files(fileLoop)
Dim fileName As String
If file.ContentLength > 20971520 Then
Response.Write("The upload failed because the file size is too large - 20MB is the limit.")
Else
fileName = HttpUtility.UrlDecode(file.FileName)
Dim ext As String = fileName.Substring(fileName.Length - 4, 4).ToLower
If ext = ".jpg" Or ext = ".gif" Or ext = ".png" Or ext = ".bmp" Or ext = ".psd" Or ext = ".tif" Then
If InStr(fileName, "\") > 0 Then
Dim arr() As String = Split(fileName, "\")
fileName = arr(arr.Length - 1)
End If
file.SaveAs(String.Format(ConfigurationManager.AppSettings("ArtworkUploadPath"), fileName))
success = True
Else
Response.Write("The upload failed because the file was the wrong type. Only files with the following extensions are allowed: .jpg, .gif, .png, .bmp, .psd, .tif")
End If
End If
Next
If success Then Response.Write("Success")
End If
Catch ex As Exception
End Try
Short answer: you can't.
Longer answer: When an upload times out, it's because the server decides your code is taking too long, kills your process, and then throws an exception. Thus your code is no longer running when the timeout occurs, so there's no way to display a message to the user.
You can log the timeout if you want, as explained in a similar question.
Ideally, you don't want to catch the timeout anyway, you want to fix your page so it doesn't time out. You can do this a couple ways:
Change the value of maxRequestLength in your system.web to limit the allowed filesize, and show an error page to the user when their upload exceeds it.
Extend the timeout period by increasing the value of executionTimeout in your system.web, as in the answer found here.
If Request.QueryString("ID") = "" Then
folderDirectory = Global.FileUpload.GetFolderDirectory(Request.QueryString("TFID"))
If Not File.Exists(folderDirectory + fileName) Then
If Not Directory.Exists(folderDirectory) Then
Directory.CreateDirectory(folderDirectory)
End If
Dim bufferSize As Integer = Me.fileUpload.PostedFile.ContentLength
Dim buffer As Byte() = New Byte(bufferSize) {}
' write the byte to disk
Using fs As New FileStream(Path.Combine(folderDirectory, fileName), FileMode.Create)
Dim bytes As Integer = Me.fileUpload.PostedFile.InputStream.Read(buffer, 0, bufferSize)
' write the bytes to the file stream
fs.Write(buffer, 0, bytes)
End Using
Else
CallOnComplete("error", "", "Error uploading '" & fileName & "'. File has been exists!")
Exit Sub
End If
But Fortify scan report for the above sample code shows Path Manipulation issue as high. I Need help to modify above code so that it can pass fortify scan
It is showing me error at folderDirectory
Usually, when your code works inside a web application you don't have the liberty to use the full file system as you do on your local PC. Any kind of 'Path Manipulation' is suspicious.
You should try to recode your works using Server.MapPath method.
Pay particular attention to this warning
For security reasons, the AspEnableParentPaths property has a default value set to FALSE.
Scripts will not have access to the physical directory structure unless AspEnableParentPaths
is set to TRUE.
I am implementing telerik RadUpload in my asp.net web application.I added corresponding the handler and module entries in web.config.
<add path="Telerik.RadUploadProgressHandler.ashx"
type="Telerik.Web.UI.RadUploadProgressHandler" verb="*" validate="false" />
<add name="RadUploadModule"
type="Telerik.Web.UI.RadUploadHttpModule" />
I have a functionality where I need to upload excel file and need to see the progress bar while uploading until it completes 100%.
PROBLEM: I am wondering how to capture the percentage of file uploaded and show it in progressarea.
MY CODE (Button_Click):
Const total As Integer = 100
Dim progress As RadProgressContext = RadProgressContext.Current
progress.Speed = "N/A"
Dim files As UploadedFileCollection = RadUpload1.UploadedFiles
Dim up As RadUpload = RadUpload1
If files IsNot Nothing AndAlso 0 <> files.Count Then
For i As Integer = 0 To total - 1
progress("SecondaryTotal") = total.ToString()
progress("SecondaryValue") = i.ToString()
progress("SecondaryPercent") = i.ToString()
progress("CurrentOperationText") = files(0).GetName() & " is being processed..."
If Not Response.IsClientConnected Then
Exit For
End If
progress.TimeEstimated = (total - i) * 100
---------ACTUAL UPLOAD FUNCTIONALITY HERE----------
objUpload.CreateBulkUploadRequest(bytes)
Next
End If
Private Sub CreateBulkUploadRequest(bytes)
StoreDocumentinImageServer(bytes)
End Sub
Public Function StoreDocumentinImageServer(ByVal PostData As Byte()) As Integer
Try
Dim req As HttpWebRequest
Dim resp As HttpWebResponse
Dim postStream As Stream
Dim respStream As StreamReader
Dim Url As String
Dim response As String = String.Empty
Dim ImageId As Integer = 0
Dim qryString As New StringBuilder("?fileSize=")
qryString.Append(PostData.Length)
qryString.Append("&userId=" + RequestedBy.ToString)
qryString.Append("&applicationName=" + RequestType.ToString)
qryString.Append("&imageName=" + FileName)
qryString.Append("&mode=Insert")
Url = ImageServiceUrl + qryString.ToString
req = CType(WebRequest.Create(Url), HttpWebRequest)
req.Method = "POST"
req.ContentType = contenttype
req.KeepAlive = True
req.ContentLength = PostData.Length
postStream = req.GetRequestStream()
postStream.Write(PostData, 0, PostData.Length)
resp = CType(req.GetResponse(), HttpWebResponse)
respStream = New StreamReader(resp.GetResponseStream(), Encoding.Default)
response = respStream.ReadToEnd()
respStream.Close()
resp.Close()
Catch ex As Exception
Throw ex
End Try
End Function
PROBLEM---- Now CreateBulkUploadRequest() method is Synchronous , it will take 10 min to upload and finally come out of the method execution. Now mean while how would I update the progress area and percentage of file upload status.
My biggest problem is CreateBulkUploadRequest() is in the loop of progress bar update code.
so it calls as many times it is trying to update the progress area.
AM I DOING CORRECT ????????
Please Let me know If my question is not clear.
Looking forward for any suggestions.
You don't have to handle the display of progress information yourself, it should be done automatically. Have a look at this sample code.
If you are just using the RadUpload and progress area to check the uploaded % then you do not need any additional code in the code-behind. The code (markup) mentioned in this demo should suffice.
However, If you want some custom progress monitoring, which it seems that you are doing with the code provided, you will need to go about this slightly differently. This demo covers just how custom progress monitoring should be implemented. I would double check that the code that you have implemented aligns with the sample in that demo.
I made a very very simple small app to take screenshot of the desktop and send to network share. About 10 PC's would have this app installed.
My idea is, that there will be one dashboard in ASP.NET, which simply shows those screenshots on the webpage. So far, easy stuff.
But, because I don't want to clog the network and send the screenshot every 1 minute, I would like to launch the .exe on the remote PC's by demand of ASP.NET user.
Unfortunately I haven't found any information (and I'm a complete ASP.NET n00b), how to launch remote executable IN the context of the remote PC (so I won't see screenshots of ASP server :) )
If there is no such possibility, please advise about other way to solve this.
Update after clarification:
Take a look at the situation from another angle:
Why don't you run a web server on the clients that host an asp.net page that triggers the capture. Then you can, from your root server, simply sent http requests to the clients and fetch the image.
You can try http://CassiniDev.codeplex.com - it supports external IP and hostnames.
And you may also consider simply embedding the CassiniDev-lib (a very simple example is shown here - Using CassiniDev to host ASP.Net in your application, that way you can use the web server as the reciever and the forms app can do whatever it wants on the client.
I am confident in this approach as I designed cassinidev with this as one of the primary use cases.
From asp.net you cannot. It is only HTML/JavaScript once it gets to the browser.
ActiveX is a possibility but it is quite painful and dated and limited. And painful.
The new way to do something like this is to deploy a .net Forms application or WPF app via Click Once.
You can also write a WPF Browser Application but getting the kind of permissions you would need would entail setting the site as full trust.
If a web page could launch an arbitrary .exe file on your machine, that would be a security disaster.
However, since these are your PCs, you can require them to install an ActiveX control of some kind that you could then embed in your ASP.NET page.
As others have said, there is really no way for ASP.Net to call out to the apps, but reversing the control flow should work OK...
I suppose you could have the grabber application running all the time on the users desktop, but have it make a call to a web service / file served by the server that contains an instruction for that instance of the app to grab a screenshot.
Something like...
App : Do I have to do anything? (GET /workinstruction.aspx)
Server : no. (server decides whether to request work, and return the result in (workinstruction.aspx)
App : (waits 1 minute)
App : Do I have to do anything?
Server : yes.
App : (takes screenshot and submits)
App : (waits 1 minute)
App : Do I have to do anything?
etc...
Thank you all for answering, those were interesting approaches to the subject.
Yet due to many factors I ended up with following solution:
Pseudo-service (Windows Forms with tray icon and hidden form) application on client PC's. It is serving as TCP server.
ASP.Net web app on the server, with TCP client function.
On request of the web user, web app is sending preformatted TCP 'activation' string to the chosen PC. Tray app is making a screenshot and sending it to predefined SMB share, available for web app to display.
Thanks again!
I've done this exact thing a few times for monitoring remote display systems. What I found was that using MiniCap.exe to capture image also took video (which was required on remote display systems).
I also used Cassini as described by Sky Sanders with an ASPX-page with the following code.
Then I just reference the page from an img src="http://computer/page.aspx?paramters". (Let me know if you need more info)
<%# Import NameSpace="System.IO" %>
<%# Import NameSpace="System.Drawing" %>
<%# Import NameSpace="System.Drawing.Imaging" %>
<%# Import NameSpace="System.Diagnostics" %>
<%
Response.Buffer = True
Response.BufferOutput = True
Dim CompressionLevel As Integer = 1
Dim compress As Integer = 1
If Not Request.Item("compress") Is Nothing Then
If IsNumeric(Request.Item("compress")) = True Then
CompressionLevel = CInt(Request.Item("compress"))
End If
End If
compress = CompressionLevel
' Resize requested?
Dim SizeX As Integer = 100
Dim SizeY As Integer = 75
If Not Request.Item("width") Is Nothing Then
If IsNumeric(Request.Item("width")) = True Then
SizeX = CInt(Request.Item("width"))
CompressionLevel = 10
End If
End If
If Not Request.Item("height") Is Nothing Then
If IsNumeric(Request.Item("height")) = True Then
SizeY = CInt(Request.Item("height"))
CompressionLevel = 10
End If
End If
Dim Region As String = ""
If Not Request.Item("region") Is Nothing Then
Region = Request.Item("region")
End If
Dim XS As Integer = 0
Dim YS As Integer = 0
Dim XE As Integer = 1023
Dim YE As Integer = 766
Try
If Region.IndexOf(",") > -1 Then
Dim Rec() As String = Region.Split(",")
If Rec.GetUpperBound(0) >= 3 Then
If IsNumeric(Rec(0)) Then XS = Rec(0)
If IsNumeric(Rec(1)) Then YS = Rec(1)
If IsNumeric(Rec(2)) Then XE = Rec(2)
If IsNumeric(Rec(3)) Then YE = Rec(3)
End If
End If
Catch : End Try
Dim FileType As String = "jpg"
Dim MimeType As String = "jpeg"
If Not Request.Item("filetype") Is Nothing Then
FileType = Request.Item("filetype")
MimeType = FileType
End If
If Not Request.Item("mimetype") Is Nothing Then
FileType = Request.Item("mimetype")
End If
Dim ImageFile As String = ""
Dim ImageThumbFile As String = ""
Dim ImageFolder As String = Server.MapPath("~/ScreenShots/")
If IO.Directory.Exists(ImageFolder) = False Then
IO.Directory.CreateDirectory(ImageFolder)
End If
' Delete files older than 30 minutes
For Each File As String In IO.Directory.GetFiles(ImageFolder)
Response.Write("File: " & File & "<br>")
If IO.File.GetCreationTimeUtc(File).AddMinutes(30) < Now.ToUniversalTime Then
IO.File.Delete(File)
End If
Next
' Find available filename
Dim tmpC As Integer = 0
While tmpC < 100
tmpC += 1
ImageFile = "ScreenShot_" & CStr(tmpC).PadLeft(5, "0") & "." & FileType
ImageThumbFile = "ScreenShot_" & CStr(tmpC).PadLeft(5, "0") & "_thumb." & FileType
If IO.File.Exists(ImageFolder & "\" & ImageFile) = False Then
' Found our filename
' Reserve it
Dim ios As IO.FileStream = IO.File.Create(ImageFolder & "\" & ImageFile)
ios.Close()
ios = Nothing
Exit While
End If
End While
' Run MiniCap
' " -capturedesktop" & _
Dim CMD As String = """" & Server.MapPath("/MiniCap.EXE") & """" & _
" -save """ & ImageFolder & "\" & ImageFile & """" & _
" -captureregion " & XS & " " & YS & " " & XE & " " & YE & _
" -exit" & _
" -compress " & CompressionLevel
If Not CMD Is Nothing Then
Dim myProcess As Process = New Process
Dim RouteFB As String
With myProcess
With .StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
End With
.Start()
End With
Dim sIn As IO.StreamWriter = myProcess.StandardInput
sIn.AutoFlush = True
' Create stream reader/writer references
Dim sOut As IO.StreamReader = myProcess.StandardOutput
Dim sErr As IO.StreamReader = myProcess.StandardError
' Send commands
sIn.Write(CMD & System.Environment.NewLine)
sIn.Write("exit" & System.Environment.NewLine)
' Wait one second
'Threading.Thread.CurrentThread.Sleep(60000)
' Read all data
Response.Write(sOut.ReadToEnd)
' Kill process if still running
If Not myProcess.HasExited Then
myProcess.Kill()
End If
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()
End If
Response.Clear()
Response.ClearContent()
If Not Request.Item("width") Is Nothing Or Not Request.Item("length") Is Nothing Then
' Resize, making thumbnail in desired size
Dim b As Bitmap = Bitmap.FromFile(ImageFolder & "\" & ImageFile)
Dim thumb As Bitmap = b.GetThumbnailImage(SizeX, SizeY, Nothing, IntPtr.Zero)
' Jpeg image codec
Dim jpegCodec As ImageCodecInfo
' Get image codecs for all image formats
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
' Find the correct image codec
For i As Integer = 0 To codecs.Length - 1
If (codecs(i).MimeType = "image/" & MimeType) Then
jpegCodec = codecs(i)
Exit For
End If
Next i
Dim qualityParam As New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compress * 10)
Dim encoderParams As New EncoderParameters(1)
encoderParams.Param(0) = qualityParam
thumb.Save(ImageFolder & "\" & ImageThumbFile, jpegCodec, encoderParams)
thumb.Dispose()
b.Dispose()
' Send thumb
Response.TransmitFile(ImageFolder & "\" & ImageThumbFile)
Else
' Send normal file
Response.TransmitFile(ImageFolder & "\" & ImageFile)
End If
Response.End()
%>