So, as I figured out, when I have a form with enctype="multipart/form-data" and I upload a file, I can no longer access the object request. The following error is shown:
Cannot use the generic Request collection after calling BinaryRead.
After checking some resources, I stumpled upon a statement, which says: "This is by design". Well, okay, not here to judge about design-decisions.
To give you a quick overview, let me walk you through the code:
if request("todo") = "add" then
Set Form = New ASPForm
category = request("category")
title = request("title")
if len(Form("upload_file").FileName) > 0 then
filename = Form("upload_file").FileName
DestinationPath = Server.mapPath("personal/allrounder/dokumente/")
Form.Files.Save DestinationPath
end if
end if
Nothing too special here so far. Later however, when I try to access my request object, the error mentioned above occures:
<% if request("todo") = "new" then %>
...
My question now, how to get rid of it or fix this. I don't want to open the upload in a popup if there is another way around. This is the only solution I could think off.
Perfectly would be an object, which checks Form and request. Alternatively maybe a check at the top of the file, which object I have to use?
Thanks for any suggestions.
There used to be a very popular ASP class/component that solved ASP file uploads. The site for that component has been taken down, but the code is mirrored here:
https://github.com/romuloalves/free-asp-upload
You can include this ASP page on your own page, and on your page instantiate the class to get access to the files in your form, but also to the form variables. Here is a piece of example code (Upload.Form accesses the form fields):
Dim uploadsDir : uploadsDir = server.mapPath(".") ' whatever you want
Dim Upload, ks, fileKey, mailto
Set Upload = New FreeASPUpload
call Upload.Save(uploadsDir)
ks = Upload.UploadedFiles.keys
for each fileKey in ks
Response.write(fileKey & " : " & Upload.UploadedFiles(fileKey).FileName & "<br/>")
next
mailto = Upload.form("mailTo")
Set Upload = Nothing
If you want to stick to your own implementation, you can probably figure out how to get to the form variables in a multipart/form-data encoded data stream by having a look at the code they use to do so.
Related
Does this redirection method have a specific name, and how do I set it up for an ASP based site?
http://www.example.com/?URL=www.redirecteddomain.com
Ok, in that case, it not really the web server, but simply your code that can do this.
so, if you web page was:
http://localhost/MyJumpPage.aspx?URL=www.google.com
So, in your code, all you have to do is grab that 1st parameter, and then run code to jump/navigate to that page.
EG:
string strURL = "";
strURL = Request.QueryString("URL");
Response.Redirect("http://" + strURL);
So, the code behind a button, or even on page load can simply pull the query value from the url string, and then jump to that URL.
I want to upload a file from codebehind.
I would like to explain my situation a bit.
My existing code is..
I have a fileupload control.
<asp:FileUpload ID="fuSRForm" runat="server" class="file"/>
Then from the codebehind, I check the conditions for that control.
if (fuSRForm.HasFile && (fuSRForm.PostedFile.ContentType.ToString().Trim().ToLower().Contains("pdf") || fuSRForm.PostedFile.ContentType.ToString().Trim().ToLower().Contains("application/vnd.openxmlformats-officedocument.wordprocessingml.document") || fuSRForm.PostedFile.ContentType.ToString().Trim().ToLower().Contains("msword")))
And if the conditions are met,
I used the Sitecore API to upload the file.
This is the part of uploading API.
// creating necessary arguments to be passed to the processor
UploadArgs args = new UploadArgs();
// adding http files collection
args.Files = base.Request.Files;
So, the API is grabbing all posted files by using base.Request.Files
My new situation is that I creat a pdf file when the user click Submit.
Then I save it in a folder named asyncupload
After that I have to upload it to Sitecore using the same API
So, I tried to change the base.Request.Files to my file.
But I am unable to change that.
So, I will have to upload my file using FileUpload control.
FileUpload tempFU = new FileUpload();
tempFU.PostedFile= ????
args.Files = base.Request.Files;
I am stuck right here. I can either post my file from code behind or change the base.Request.Files to my file.
either way, I am stuck. Anyone can solve that?
I'm not sure what your end goal here is. Maybe you could post some more code if I've understood you incorrectly. But here's how to add a file to the Media Library: Brian Pedersen: Adding a file to the Sitecore Media Library programatically
If you are using an <asp:FileUpload /> control, then you can save the file to server disk like this:
string filePath = Path.Combine(Server.MapPath("~/"), "upload/tempfile.tmp");
FileUpload1.SaveAs(filePath);
Then call the AddFile method from the link I provided above:
AddFile(filePath);
You need to use handler in the api you are going to hit and hit it by
using (WebClient client = new WebClient())
{
client.UploadFile(Sitecore Handler, FilePath);
}
I've found a few posts about retrieving HTML from an ASPX page, mostly by overriding the render method, using a WebClient, or creating an HttpWebRequest. All these methods return the HTML of the page as it's loaded, but I was hoping to actually retrieve the HTML after the user has entered information.
The purpose behind this is that I work in IT, and I'm attempting to build a logging library that has an overload that essentially does a "screen-scrape" on the page just as the user encounters an exception, that way I can log the exception, and create an HTML file in a sub-directory of the logging directory that shows the page exactly as the user had it before clicking "submit" or having some other random error, and add an "ID" to the error that's logged telling whoever is fixing the issue which page to look at.
I hope I've provided enough information, because I really have no idea where to start.
Also, We'd like to do this through our own library, because our logging library is included in our common library, and many of our common library functions use our logging class.
Hmmm...
If you want to see what the user sees after they've been using the page, you're most likely going to have to do some fancy client-side scripting.
A naive approach:
When the clicks the submit button, fire a JavaScript event that encodes the DOM and either passes it as a form variable to the server, or executes a separate AJAX request with the encoded data as a parameter. ("Encode" in this case may be as simple as grabbing document.innerHtml, but I haven't checked.)
This potentially introduces a lot of overhead to every form submission, so I'd keep it out of production code.
I'm not sure why you need the rendered HTML as part of your exception log - I've never found it necessary for server-side debugging.
You getting HTML code from a website. You can use code like this.
string urlAddress = "http://www.jobdoor.in";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
string data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
I want to create an html page inside a asp.net page using c# and then request that html page. The flow is, I'll be creating a request that will give me a response with some values. Those values will be stored in hidden fields in the html page I'm creating on the fly and then requesting. I figure it would be something like below but I'm not sure if it would work, I've also received some "Thread Aborting" errors. So, does anyone know the proper way to do this or at least direct me to a nice article or something?
StringBuilder builder = new StringBuilder();
builder.Append("<html><head></head>");
builder.Append("<body onload=\"document.aButton.submit();\">");
builder.Append("<input type=\"hidden\" name=\"something\" value=\"" + aValue + "\">");
HttpContext.Current...Response.Write(builder.ToString());
... end response
This is a very common request and is almost never a good idea. What are you trying to do?
That said: you write out a file with a temporary name and redirect to that file. Later you have to figure out when it's safe to delete the file.
Edit That method points out one of the problems: you have to do your own garbage collection, deciding how long files must be kept around and deleting them appropriately.
I have a web control with some custom javascript. The javascript creates new file upload controls on the client dynamically using code similar to:
var newFileUpload = document.createElement('input');
newFileUpload.type = 'file';
container.appendChild(newFileUpload); // where container is a div
This exists in an ASP.NET form with encType set to multipart/form-data. I will have 1 - n controls on the page (well, limited to a reasonable number, of course).
I now would like to capture the uploaded files in my ASP.NET application. Because of the approach taken above I know that I cannot capture them as I would from a FileUpload control (which unfortunately I cannot use). Is there another way to capture the uploaded files?
I have looked through a number of areas, including:
Request.Files
Request.Form
Request.Form.Keys
Request.InputStream
But I have not been able to find the content. I believe the client is submitting this data correctly but have been unable to determine what the server does with the raw request information (if this is even exposed).
Does anyone have any suggestions on areas I might further explore?
Thanks
You should add a unique name to your upload element to get it from Request.Form collection.
var newFileUpload = document.createElement('input');
newFileUpload.type = 'file';
//newFileUpload.id = 'file01';
newFileUpload.name = 'file01';
container.appendChild(newFileUpload);
EDIT : I tried id and name attibutes, the with name, you can get the content by
Request.Form["file01"]
Also if you should add the attribute below to your form element. This allows you to get the file content by Request.Files["file01"] :
enctype="multipart/form-data"