I have a web page which I want people to be able to upload content to. (There will only be a small number of people using it, as it is access restricted, so I'm not too worried about any DOS type attacks.)
I'm using a fileUpload control to do this:
protected void Button1_Click(object sender, EventArgs e)
{
if (fileUploader.HasFile)
try {
fileUploader.SaveAs(Server.MapPath("Uploads\\") + fileUploader.FileName);
errorMessage.Text = "File name: " +
fileUploader.PostedFile.FileName + "<br>" +
fileUploader.PostedFile.ContentLength + " kb<br>";
}
catch (Exception ex) {
errorMessage.Text = "ERROR: " + ex.Message.ToString();
}
else
{
errorMessage.Text = "You have not specified a file.";
}
}
The files can be up to 50MB (I have changed the web.config to allow this). The problem I have is that with large files the user can't see the progress of the upload.
I want to know how I can display the progress on the page so the user can see something is happening. Not fussed about anything fancy - just something like:
bytes uploaded / total bytes
would be fine. I can get the total bytes using postedFile.ContentLength, but don't know how to get the bytes uploaded.
Also - am I able to refresh the screen as the upload is taking place?
Cheers,
Ben
This answer is not specific to ASP...
A file upload is typically done by making an HTTP POST request with Content-type: multipart/form-data. This includes the file body in one of the parts of the request body.
So within a given browser tab, there can only be one page loading at a given time. The short answer is, unless you get really fancy with iframes (one for status, one for upload), you will not be able to show the status.
However, if you use Flash, you can. http://www.uploadify.com/ is a great little front-end plugin (for jQuery) which allows the user to select multiple files, and uploads them all, showing the status along the way. I just plugged it into a site that limits uploads to 64M. Works awesome.
Your implementation is a synchronous operation which does not 'feedback' to the user about the file upload progress.
There are quite a few alternatives and 3rd party components, you might want to google them.
Here's one to start with: ASP.NET File Upload with Real-Time Progress Bar
I have used a couple of commercial components to do this:
Telerik and AjaxUPloader
Related
I am trying to import data for custom dimension in Google Analytics through the .NET client library. In Google Analytics, when I view the uploads for a data set from Admin > Data Import > Manage Uploads, it says my uploads are successful, but the data for the custom dimension doesn't seem to show up in my report. Right now, I am just using my custom dimension to set the category for an article.
Here is how I am uploading through the .Net client library.
string accountId = "***";
string webPropertyId = "***";
string customDataSourceId = "***";
string contentType = "application/octet-stream";
IUploadProgress progress;
using (var dataStream = CreateArticleCsvStream(articles))
{
var fs = File.Create("test.csv");
dataStream.CopyTo(fs);
fs.Close();
progress = service.Management.Uploads.UploadData(accountId, webPropertyId, customDataSourceId, dataStream, contentType).Upload();
}
if (progress.Status == UploadStatus.Failed)
{
throw progress.Exception;
}
Here is the output for test.csv
ga:pagePath,ga:dimension1
/path/to/page/,"MyCategory"
When I download the file from the data set, I get the same file as the test.csv file, it just has a random filename that gets assisgned to it.
I found this other question similar to mine, but there was no solution posted. Any help would be appreciated.
I have also waited over 24 hours, but still nothing.
It took a few days of trial and error but I finally found the solution.
First thing to check is that your Website's URL is correct under Admin > View Settings. We had ours set up like my.domain.com/path/to/site when it should have just been my.domain.com. (We are using SharePoint, which is why path/to/site was appended to the site URL)
Second thing to check is that your key/pagePath entries are all correct. In our case, we had an extra forward slash at the end of the URL. For some reason, Google Analytics displays the trailing forward slash in reports, but does not actually store it for the pagePath.
Another error may be capitalization. It seems like GA applies filters after the data has been processed. If you add the lowercase/uppercase filter, notice that it only affects how the URLs display in your reports. Behind the scenes, it seems that GA still stores the URL with whatever capitalization the hit initially came in with. For example if the URL on your site is my.domain.com/path/to/PAGE.aspx and you apply the lowercase filter, the pagePath will display in your reports as /path/to/page.aspx. But, if you use the lowercase value in your csv import, the data will not join. You must use the pagePath that appears on your site (/path/to/PAGE.aspx in this case).
It would be nice if Google gave some log files when it tries to process and join the uploaded data with the existing data, rather than just saying the upload was successful even though the processing/joining stage may fail.
I have problem with browse button and switching to file dialog. I cannot use my file path control and just send there my string with file path and file itself, as it's readonly and in fact some behind control is my input filepath.
Here's my code
driver.FindElement(By.Id("browseButton")).Click();
driver.SwitchTo().ActiveElement().SendKeys(filepath);
Above code fills my control for file path, as i can see that on UI. But my open file dialog is still opened and i do not know how to close it and submit my upload.
Uploading files in Selenium can be a pain, to say the least. The real problem comes from the fact that it does not support dialog boxes such as file upload and download.
I go over this in an answer to another question, so I will just copy/paste my answer from there here. The code examples should actually be relevant in your case, since you are using C#:
Copied from previous answer on question here:
Selenium Webdriver doesn't really support this. Interacting with non-browser windows (such as native file upload dialogs and basic auth dialogs) has been a topic of much discussion on the WebDriver discussion board, but there has been little to no progress on the subject.
I have, in the past, been able to work around this by capturing the underlying request with a tool such as Fiddler2, and then just sending the request with the specified file attached as a byte blob.
If you need cookies from an authenticated session, WebDriver.magage().getCookies() should help you in that aspect.
edit: I have code for this somewhere that worked, I'll see if I can get ahold of something that you can use.
public RosterPage UploadRosterFile(String filePath){
Face().Log("Importing Roster...");
LoginRequest login = new LoginRequest();
login.username = Prefs.EmailLogin;
login.password = Prefs.PasswordLogin;
login.rememberMe = false;
login.forward = "";
login.schoolId = "";
//Set up request data
String url = "http://www.foo.bar.com" + "/ManageRoster/UploadRoster";
String javaScript = "return $('#seasons li.selected') .attr('data-season-id');";
String seasonId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
javaScript = "return Foo.Bar.data.selectedTeamId;";
String teamId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
//Send Request and parse the response into the new Driver URL
MultipartForm form = new MultipartForm(url);
form.SetField("teamId", teamId);
form.SetField("seasonId", seasonId);
form.SendFile(filePath,LoginRequest.sendLoginRequest(login));
String response = form.ResponseText.ToString();
String newURL = StaticBaseTestObjs.RemoveStringSubString("http://www.foo.bar.com" + response.Split('"')[1].Split('"')[0],"amp;");
Face().Log("Navigating to URL: "+ newURL);
Driver().GoTo(new Uri(newURL));
return this;
}
Where MultiPartForm is:
MultiPartForm
And LoginRequest/Response:
LoginRequest
LoginResponse
The code above is in C#, but there are equivalent base classes in Java that will do what you need them to do to mimic this functionality.
The most important part of all of that code is the MultiPartForm.SendFile method, which is where the magic happens.
One of the many ways to do that is to remove the disable attribute and then use typical selenium SendKeys() to accomplish that
public void test(string path)
{
By byId = By.Id("removeAttribute");
const string removeAttribute = #"document.getElementById('browseButton').removeAttribute('disabled');";
((IJavaScriptExecutor)Driver).ExecuteScript(removeAttribute);
driver.FindElement(byId).Clear();
driver.FindElement(byId).SendKeys(path);
}
You can use this Auto IT Script to Handle File Upload Option.
Auto IT Script for File Upload:
AutoItSetOption("WinTitleMatchMode","2") ; set the select mode to
Do
Sleep ("1000")
until WinExists("File Upload")
WinWait("File Upload")
WinActivate("File Upload")
ControlFocus("File Upload","","Edit1")
Sleep(2000)
ControlSetText("File Upload" , "", "Edit1", $CmdLineRaw)
Sleep(2000)
ControlClick("File Upload" , "","Button1");
Build and Compile the above code and place the EXE in a path and call it when u need it.
Call this Once you click in the Browse Button.
Process p = System.Diagnostics.Process.Start(txt_Browse.Text + "\\File Upload", DocFileName);
p.WaitForExit();
I'm currently developing a website and having trouble finding the right solution.
When I open my site from a different browser and tried to make an AJAX call at the same time, I get an error, because both the browsers are processing the request.
Is there a way to detect if the server is currently executing an AJAX call and wait until it finishes the first request before executing the second one?
here is my code when i tried to make an ajax request at the same time using different browsers
using (StreamWriter sw = new StreamWriter(hhpPath + "\\Contents\\" + Path.GetFileName(hhpPath).ToString() + ".hhp", false))
{
sw.WriteLine("[OPTIONS]");
sw.WriteLine("Compatibility=1.1 or later");
sw.WriteLine("Compiled file=" + hhpPath + "\\Unpublished\\"+ Name +".chm"); //the file to be converted
sw.WriteLine("Contents file=" + (hhpPath + "\\Contents\\" + Path.GetFileName(hhpPath).ToString()) + ".hhc"); //TOC file
sw.WriteLine("Default topic=" + "Installing the Prototype Terminal.html");
sw.WriteLine("Display compile progress=no");
sw.WriteLine("Index file=" + (hhpPath + "\\Contents\\" + Path.GetFileName(hhpPath).ToString()) + ".hhk"); //index file
sw.WriteLine("Title=none");
sw.WriteLine("\r\n[FILES]");
sw.WriteLine("none");
}
the error says that the file is currently on use.
The fact that it is an AJAX call via jQuery is irrelevant. You problem is: your website tries to exclusively access a resource (in your case, write to a constant file) on a request.
Since the website is designed to simultaneously serve a lot of clients, you will inevitably run into access issues.
The functionality you're trying to achieve isn't really typical for a website. Could you please describe what problem you are trying to solve, so that we could give you a better advise?
Possible solutions could be a combination of:
Write to a different file on each request
(as suggested in another answer) Gracefully handle the file access error: either wait until it's free, or return an error
Use a database instead of the file system for writing data
Limit this feature to specific clients
Remove this feature altogether
You need to lock the file while writing content in file as like below
lock (lockObject)
{
var sw = new StreamWriter(fs);
sw.Write('text');
sw.Flush();
sw.Close();
}
Please check this link for thread synchronization.
When you make ajax request its not directly possible to detect on client side if file is in use . You can put a try catch around that item and return a -1(file locked) status 1(success) or 0(some other error).
Or try following function to check before your code to see if file is in use and return related status to ajax calls.
protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
Boolean status = false;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
status = false ;
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
status = true;
}
finally
{
if (stream != null)
stream.Close();
}
//file is not locked
return status;
}
I'm currently using an html embed tag to display a pdf file that is saved on the local server. Is there a wayo to display a pdf file on my page without having to save it to the local file system of the server? I just wand to pass it to the view from the controller in such a way that it can be displayed as a pdf in the page without having it stored on the file system directly.
Alternatively, is there a way to call a method to delete the pdf file from the server once the user has navigated away from the page they are viewing? How do I tell if th euser has navicated away from the page and how do i cause that to trigger a method that will delete the file?
I created a MVC class called PdfResult that returns a byte array as a PDF file.
The purpose is as follows (can't upload the source code, sorry):
PdfResult inherits from FileStreamResult
Set the Content-Type header to application/pdf
Set the Content-Disposition to either attachment or inline, and set an appropriate file name
Convert your data to a Stream -- if your data is a byte array, then write it to a MemoryStream.
See https://stackoverflow.com/a/16673120/272072 for a good example of how to do this.
Then, your embed code just needs to point to the action method, as if it was a PDF file.
Here's an example:
public ActionResult ShowPdf() {
// Note: the view should contain a tag like <embed src='MyController/GetPdf'>
return View();
}
public ActionResult GetPdf() {
byte[] pdfBytes = dataRepo.GetPdf(...);
return new PdfResult(pdfBytes, "Filename.pdf", false) ;
}
Here is a link to a CodeProject article and code sample titled Download and Upload Images from SQL Server via ASP.NET MVC. This gives an example of an efficient method to stream content to and from SQL Server via MVC.
You can easily adapt the code to stream your PDF file downloads.
UPDATE
The article uses a DataReader, but it can easily be adapted to Linq2Sql or EF. As an example, here is the Read method where I am reading from the database and copying to the stream:
public override int Read(byte[] buffer, int offset, int count)
{
result = _attachments.ExecuteStoreQuery<byte[]>(
"SELECT SUBSTRING(AttachmentBytes, " + position.ToString() +
", " + count.ToString() + ") FROM Attachments WHERE Id = {0}",
id).First();
var bytesRead = result.Length;
Buffer.BlockCopy(result, 0, buffer, 0, bytesRead);
position += bytesRead;
return (int)bytesRead;
}
You can read the PDF as a bytestream from the database and save it to the http response stream. If you have set the content type correctly to application/pdf, then the browser will load the document in the PDF plugin.
Update (14/Oct/2011): You need to write the bytestream to the Response.OutputStream object. How you create and write the byte stream is dependent on how you have stored in the database and how you are retrieving it. The following code snippet is from an article we have on our website - Generate PDF Forms In ASP.NET Using PDFOne .NET v3.
// Get the page's output stream ready
Response.Clear();
Response.BufferOutput = true;
// Make the browser display the forms document
// using a PDF plug-in. (If no plug in is available,
// the browser will show the File -> Save As dialog box.
Response.ContentType = "application/pdf";
// Write the forms document to the browser
doc.Save(Response.OutputStream);
doc.Close();
doc.Dispose();
The doc object is from our component. You need not use that. This code snippet is only for your understanding. For your requirement, you may have to something like bytestream.save(Response.OutputStream) I guess. BTW, this code is for ordinary ASP.NET, not MVC.
DISCLAIMER: I work for Gnostice.
If you want to create the PDF 100% dynamically, you would generate it completely in memory then stream it out directly to the requesting web browser without saving it as a file. This is very easy to do with the right tools. I would recommend AspPDF from Persits.com as a way to do this very easily. Take a look at their online documentation to see how simple this is to do without creating a bunch of rendered PDF files all over your server.
If you cannot do something like that, then simply incorporate a process to cleanup your "expired" PDF files from your server's filesystem based on their age. For example, after you have created your local PDF file, you just look through the folder containing your temporary PDF's and delete any you find over a certain age. You cannot reliably tell if or when a user has navigated away from your page or site.
For the first part of your question, like mentioned in the comments, use some type of stream object to pass the PDF data around. Right now, you are streaming the file to the local file system, then streaming it once again to the embedded tag for display. Just do away with the intermediate step of saving to the file system, and do the whole thing in memory (although, that's not really a model of efficiency, and might not scale well).
Regarding the second part of your question, that's not as straightforward. MVC really has no concept of state (viewstate, etc.), so it doesn't have events that can be fired from a state change (say, navigating away from a page).
You could use Javascript to detect a user navigating away from your page (windows.onunload), that calls a (C#/VB) method to remove the file from the file system. You would probably have to use AJAX to communicate back to the server, using an HTTP POST method, and have something listening at that URL endpoint to fire your method that removes the file.
I have an aspx (say 1.aspx) page from where first I am downloading a pdf file and then I want to redirect to some Thanks.aspx page. The code is this:
protected void btnSubmit_Click(object sender, EventArgs e)
{
string pathId = string.Empty;
if (Page.IsValid)
{
try
{
pathId = hidId.Value;
DownloadPDF(pathId);
Response.Redirect("Thanks.aspx");
}
catch (Exception ex)
{
throw ex;
}
}
}
protected void DownloadPDF(string pathId)
{
if (!(string.IsNullOrEmpty(pathId)))
{
try
{
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + pathId + ".pdf");
string path = ConfigurationManager.AppSettings["Pdf_Path"].ToString() + "\\" + pathId.Trim() + ".pdf";
Response.TransmitFile(path);
}
catch (Exception ex)
{
throw ex;
}
finally
{
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
The problem is that, the file save dialog is coming properly and I am able to download the file also, but it is not getting redirected to the Thanks.aspx page.
How to resolve this?
If the file is just downloaded, no preprocessing is done, you could try the following:
Response.AddHeader("Refresh", "12;URL=nextpage.aspx");
Where the number is the seconds before refresh is done :)
I've found it easier to put the PDF download page in an iframe. That way you can activate the PDF download on the client side by just pointing the iframe source to the PDF download page. After that you can either move to a new page, or just show the thank you text right that on the page that has the iframe in it.
In HTTP, a request can only have a single response. Since the first response is the PDF file, the seconds response (i.e. the redirect) cannot be implemented.
You can try to redesign the two pages by redirecting to thanks.aspx and have thanks.aspx start the download automatically.
A Response.Redirect actually sends a response to the browser that basically says this resource has moved to some other URL. However, you're trying to send a file down in a response too, so those two things are probably clashing with each other. Try sending back a little JavaScript that sends them to the page you want to send them too instead of using a Response.Redirect.
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "redirectScript", "window.location.href='whateverurlhere.aspx';", True)
See the article mentioned in this accepted answer: https://stackoverflow.com/a/11018277/1037864
(direct link: http://gruffcode.com/2010/10/28/detecting-the-file-download-dialog-in-the-browser/)
The idea is to set a cookie and send it together with the file. Meanwhile you let the waiting page block the UI while it is waiting for the cookie to arrive.
I tried many things (like the ideas here) but nothing worked for my particular situation. In the end for me I used an approach where my C# sets a cookie that the JavaScript looks for and the form buttons/etc are disabled accordingly until the cookie is detected.
My code is here in case anyone thinks this solution might work for you:
https://gist.github.com/cemerson/9811a384d7f41bc683b2bd9ed4bf5b17