i have a upload form in my web site, that allow the users to insert some items(usually trees), and they have the option to upload images after they added this items.
to upload image its take something like 15 second, but i want let the users the option to add another item in the time they are waiting for the image to upload.
so i want to use thread because:
after the upload will finish, the page will refresh and the form will reset, so they have to wait untill its will finish to upload if they want to add another item.
i tried this code:
protected void UploadFile(Object s, EventArgs e)
{
Thread t = new Thread(delegate()
{
//code for upload the image
)};
t.Start();
}
but its not working, and its even not uploading the file now.
some one know the reason?
thanks.
Your server side code is called only after the complete request is received.
So using a thread there won't help much.
If you want to keep your page interactive even while the upload happens you should look for an ajax based solution.
A very simple option is uploadify. You can hook it up to an HttpHandler and have your uploads happen in the background while the application remains interactive.
The chunk of time necessary to upload a file is not in the handling of it -- you are probably just saving the file somewhere -- it's the transmission of the file over the network.
So I would advise not to try to do background processing here, just handle it in the normal way, it will be just as performant. The user will just have to wait until the image is sent over the network.
If a user needs to add another item while his image is uploading, you can always let your site open another page in a tab/window on the browser.
maybe you can take a look at what HTML5 can do for you in terms of giving more feedback to the user that his file is been uploaded. Like Roy pointed out, transmission time is important and the users are going to have to wait anyway. But with HTML5 perhaps you can provide some nice progress bar to improve the user experience of your upload page.
Hope this helps.
Related
Am fairly new to Web Development, and am currently building a website for client using Angular 5 front end, c# back end, using ASP.NET Core. The issue I'm having is I can pass the file and upload it, but want some way of tracking the upload process, as before I upload the file I run a whole bunch of formatting checks which can take anywhere between 10-15 minutes due to the size of the file.
Is there a way to have two HTTP requests, one which will start the process and return an indicator that the process has begun and another which can be called periodically from the front end, and provide a status update on the validating taking place.
Thanks in advance!
First of all - there is something wrong if you need to wait 15-20 minutes - you probably need to create separate work thread for this (Search for background work for asp.net core) and broadcast message to the clients after using something like SignalR.
About progress - JQuery deferred supports progress method. I believe there is an implementation for promises. And SignalR supports it as well.
Next, you need to implement it on the server side somehow like that
https://blogs.msdn.microsoft.com/dotnet/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis/
Progress example:
https://www.codeproject.com/Articles/1124691/SignalR-Progress-Bar-Simple-Example-Sending-Live-D
Hope its enough to get started.
I created an ASP .NET Web Forms application from Visual Studio 2017 creator with C# as a language. I did minimum changes to the template and my web page loads a 3D image, takes a few additional parameters via Buttons and Text fields, sends it for processing to server. There it lands in a directory where it is processed by a commercial program, independent of Visual Studio, which initially writes to a directory with an image ID (say ABCD) and into a directory with an image taking date (say 20170629), after some processing, 2-5 minutes, it writes the results of the analysis into a new folder with a current date (say 20171031) into a text file where the name consists of 15 digits code and a known extension (say 123456789012345.txt). It is within Java Derby database, for which I did not find any connectors from .NET.
The web page meanwhile shows the summary of data sent. What I got working is the Button which while pressed checks if the mentioned directory (20171031) is already created shows the asp:UpdatePanel with progress (1/3 stages passed, 2/3 stages passed etc.) and when the file 123456789012345.txt is ready it gets the data from it and on the server side it calculates some values and displays them. Because of waiting there are several public async Task and public async Task methods to search for file appearance.
I wanted to avoid manual pressing of the Button, so that the processing stage is shown and once the file 123456789012345.txt is ready the values is calculated. For the moment I tried various ways, but without success. I prefer to avoid reloading the whole page every 30 seconds, because the user may get confused.
asp:UpdatePanel, as far as I understand needs pressing of the Button or some other interaction from the user side. If I use in aspx.cs file in Load_Page method the UpdatePanelName.Update(); for whatever reason the panel is not updated. Due to the use of await CalculateImageAsync(), the only way I found to have it in Page__Load is to have it as protected async void Page_Load(object sender, EventArg e) , but when page is loaded, it never loads. Anything else needs user interaction, which I want to avoid. I got stuck...
I am new in ASP .NET and C#, so maybe I do something wrong or miss some nice .NET method. I would be obliged for help. I have seen pages doing the job like I want, for example in the journals, where you submit a DOCX file where it is converted to PDF - once you submit it after some time you get a page with resulting PDF.
Regards, Marcin
I have a ASP.NET app that at one point generates a PDF file and loads the next page. I can easily do this with two separate buttons but it is made much tougher when I try to do this with one button.
When both are fired by the same button the PDF will download but the page will not load. I even had the thread sleep after the file was transmitted but it would wait but then stop afterwards.
I have attached the code that I have been trying to make work:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=labels.pdf");
Response.TransmitFile(Server.MapPath("~/"+randomNumber.ToString()+".pdf"));
Server.Transfer("~/createshipment.aspx", true);
You can't have two different responses from the server but you're trying to do so.
First - you'd like the server to return a PDF.
Second - you'd like the server to return the createshipment.aspx page.
This is just against the communication protocol. Probably the best solution is already presented by another user, competent_tech - you could open a new window (javascript's window.open) and this new window would return the PDF and in the same time the main window could post to the server and be redirected to the createshipment.aspx.
So in a nutshell you want to navigate to the next page that says something like "thank you for downloading this file" and start the download.
What you should do is on your button click you need to generate PDF and save it somewhere (on disk or DB - whichever is easier in your app), store the name/location of the new file (or primary key from DB) in a session variable and redirect to the next page. No reason to do transfer here. Then on that next page you should add a hidden iframe that points to your saved file.
Alternatively your button click could be just a link to the next page, which includes a hidden iframe pointing to the page that generates PDF. This is a bit simple but wouldn't work so well if you need to pass parameters from original page to the page that generates PDF.
This is because server.transfer "...terminates execution of the current page and starts execution of a new page by using the specified URL path of the page".
Your best bet is to open a new window in the client that gets the PDF and then perform whatever postback is needed to move the user to the next page.
I know this is old, but I'm just seeing it (looking for similar info myself).
I'm going to guess that this is causing issues:
Response.TransmitFile(Server.MapPath("~/"+randomNumber.ToString()+".pdf"));
You would need to map the path to the actual file and not some randomly created filename - or am I missing some steps?
I'm facing the following issue:
I have a C# asp.net file upload form with a submit button. After the form is submitted the file is uploaded and post-processing is started. The point is that the post-processing can take up to several minutes.
I would like to create some kind of asynchronous call of the post-processing function with showing information to the user.
So, the steps should be:
file form is submitted by user and upload is started
after the file is uploaded some information is shown to the user (e.g. "Processing..." or some loading-bar animation, etc.)
Meanwhile, the post-processing function is automatically started running in a background
After the post-processing function is finished the user is automatically redirected to another page
When i was searching the Internet I've found several examples but mostly only about asynchrounous call of functions, asynchrounous file upload (PageAsync method, etc.).
Any idea or techniques I should use or some tutorial?
Thanks in advance
That all depends on how fancy you want to get;
Meta-refresh that reloads the page until the background operation is finished
Some kind of ajax call that checks some resource for when the processing is done
HTML5 websockets. if supported, which it probably isn't.
Personally I would use the number 2. and use jQuery to poll the upload page every 500ms or something.
You can use AJAX
http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx
http://vinayakshrestha.wordpress.com/2007/03/13/uploading-files-using-aspnet-ajax-extensions/
http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=upload+using+Ajax&qscrl=1#sclient=psy&hl=en&qscrl=1&source=hp&q=upload+using+Ajax+in+asp.net&aq=f&aqi=&aql=&oq=&pbx=1&fp=db9c4fafd449a821
The jquery/flash control uploadify will allow you to do this easily. They also provide a method for asynchronously calling a method on the event that the file upload completes as described in this comprehensive documentation.
I have looked at a lot of places for a good example, and this is the one I like the best so far.
It does not handle the uploading, but it does a fine job at showing real progress to the user and it is not difficult to implement
http://inov8.wordpress.com/2010/08/29/c-asp-net-creating-an-asynchronous-threaded-worker-process-manager-and-updatepanel-progress-monitor-control/
I have an ASP.NET web application that uses jQuery on client side.
On one of the web forms I have a bunch of controls to fill, and an upload control.
User can run upload while filling other entries on the form.
When user saves the form, all data including file is stored into database .
The question is what to do with uploaded file while user fills the form?
I have two possible solutions, but don't know which of them is more optimal
Cache uploaded file, and later, while saving, retrieve it and store into database with all other data.
Save file in temporary folder, and then read it.
What is the best solution for this?
Thanks in advance.
I think the most appropriate solution will be storing uploaded file in cache.
Here is the code
var fileKey = Guid.NewGuid();
var fileStream = new Byte[Request.Files[0].ContentLength];
Request.Files[0].InputStream.Read(fileStream, 0, Request.Files[0].ContentLength);
Cache[fileKey.ToString()] = fileStream;
The fileKey GUID can be stored in ViewState, or sent as the response to client.
Later, when whole form will be saved, cached file can be retrieved and stored into database with other data.
The good thing about this method is that if user navigates from the page cached file will expire, thus avoiding resource flooding.
We can set expiration time using
Cache.Add(fileKey, fileStream, null, DateTime.UtcNow.AddMinutes(10), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
function.
How about:
3: Store it in the database but marked as in an incomplete state
This could be a separate table, or the same table with a status column.
I fail to see the difference between the 2 options: what do you mean by caching the file? Isn't it the same as saving to a temp folder?
AFAIK, most sites will not start uploading until the entire form is filled up (what if your user cancels, or browses away?). They then display an interim progress bar ("please wait while we're analyzing your data...").
As a general design rule, receiving partial data at any point in time may lead to inconstant data later. If you must do it in steps, change your form to a wizard.
Like other people mentioned - you can simply upload it. You can do it in many ways:
Upload the files in a temporary folder, and when they finally say save or complete - you can move the file from that temp folder to the other folder where you'd usually keep regular files.
One more thing that you can do, whenever the user clicks on upload button for temporary files, is that you can check you temp folder and clear other files which have stayed there for more than 1-2 days/weeks so that way you know for sure you are not wasting space.
Perhaps, you can also have a timer on
your website that regularly cleans up
this folder. I'd say this is more
efficient and you don't have to wait
for users to come in and click on
upload button to clean up your space.