I am using FIleUpload on an asp.net C# page and a button which upload the browsed picture. if the file being uploaded qualify some validation it is saved and instantly shown in asp.net image below the upload button. I made alot of search to find out about asp.net FileUpload control autopostback method but could not find. The problem i am having is when the user click upload button and when the page is rendered back all values in textboxes disappear and page go to the top. I want to do this task using ajax but cannot do it as FileUpload has no autopostback event. Can anybody give me some idea or any alternative way of doing this. Also any method by usisng which user will not require upload button. Thanks
You must do a full PostBack with the FileUpload Control, since it was not designed for asynchronous postback.
Look under Using the FileUpload Control in Partial-Page Updates in
MSDN FileUpload Web Server Control
But you could try using an iframe and <input type="file"/> like in this example:
Create an Ajax style File Upload
There is also an AjaxFileUpload Control from the ASP.Net Control Toolkit: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ajaxFileUpload/ajaxFileUpload.aspx
I used swfupload. Works like gmail attach. Starts uploading upon selection and can return a thumb for you to display.
Related
I am building an application that features a file uploader contained in an iframe. The uploader uses a second button to trigger the actual upload event. The upload event works, but when I click the upload button, it causes the page which contains the iframe in which my uploader is shown to refresh as well. The uploader and the offending upload button are both contained in the page being shown in the iframe, and the iframe itself is included in my main page this way:
<iframe id="uploaderFrame" src="FileUploader.aspx"></iframe>
I have tried wrapping the iframe in an UpdatePanel with ChildrenAsTriggers set to True, but the problem persists. Both my uploader page and my main page are in the same application.
The weird part is, this only happens if the user has selected files to upload. If the Upload button is clicked with no files posted in the uploader, the main page doesn't postback.
To be clear: I have zero problem with the page in the iframe doing postbacks. This is exactly what should be happening. Its the page containing the iframe doing postbacks that's the problem. How can I stop this?
EDIT: After further struggle, I have determined that the problem lies with that moment when files posted to the FileUpload control are actually saved to the server. I found that if I commented out the line of code where this actually happens, the parent page doesn't refresh when the Upload button is clicked.
The offending line looks like this:
file.SaveAs(System.IO.Path.Combine(Server.MapPath(path), name));
...where "file" is the name I give to the individual HttpPostedFile object contained in the PostedFiles collection, which is contained in the File Upload control. "Path" is the folder path to which the file should be saved, and "name" is the name under which to save the file.
So: is there another way to code this particular function that will keep the FileUploader's activity isolated to its own page, as it should be? Because I've even gone so far as to try to keep the parent page from refreshing using Javascript, and nothing's working.
EDIT 2: The issue is now resolved, but I'm still left with questions. After trying multiple alternatives (Ajax Tool Kit etc) with no success, I opened a copy of my project in Visual Studio 2017 -- I had been building it in VS2015 previously -- and spun it up in the debugger. And the bloody file uploader worked the first time. No unwanted postbacks, no hassle.
So it looks like the problem was with my development software, though I can't imagine what was going wrong exactly. But a solved problem is a problem solved.
Try that in your web.config
<configuration>
<system.web>
<xhtmlConformance mode="Legacy" />
</system.web>
</configuration>
Trying to implement the Blueimp jQuery file uploader and am having a frustrating time making it work in my ASPNET MVC3 C# web-application. Its very difficult to determine which jquery and css includes are required (and which are not) when referring to the official documentation or Blueimp questions on this site.
Can anyone provide a working implementation of a bare-bones form containing a file input selector (not multiple files), a single "Upload" button, a single "Cancel" button, and a progress bar? After selecting a single file, clicking "Upload" should fire an AJAX call to "UploadFile" in FileController (which is already coded and working, and accepts an HttpPostedFileBase parameter) and update the progress-bar, without a form postback. There is also no requirement to add the file name to a list of files to be uploaded (as the Blueimp demo demonstrates), as the user will only be able to select a single file in this project.
Thanks to anyone kind enough to put me out of my misery on this one.
This should help you: https://github.com/maxpavlov/jQuery-File-Upload.MVC3
A clean and simple implementation of blueimp fileupload v5.9 in ASP.Net MVC 3
I am trying to create a form which allow async file uploading with asp.net. I realize you cannot upload a file with ajax per se so I am examining alternatives
What is the best way to do this? Create an Iframe on the page with the entire form including the file input? Can I on the parent to the frame have the submit button which forces the frame to submit and then displays some sort of spinner to indicate file is uploading? Ideally upon completion I'd like to redirect the user to another page. Is there a somewhat easy way to do this???
Have you tried using one of the jquery plugins vice doing it by hand?
http://aquantum-demo.appspot.com/file-upload
Why not use the ASP.NET AJAX Control Toolkit's AsyncFileUpload control? It's free and works pretty well.
You could use http://jquery.malsup.com/form/#file-upload
Have it post to a page that will handle a file upload on the server side in your usual way.
I like to have the page return JSON with a success/failure flag and message, then parse the response to determine if the upload succeeded.
I am using Asyncfileupload AJAX control, and want to know if there is anyway for me to automatically reload/refresh the current page after the file upload?
The whole thing loads in an iframe so not sure how to do this.
Thanks
Behrouz
According to the documentation the OnClientUploadComplete callback javascript function will be executed when the file upload completes. So you could register for this event and refresh the current page using window.location.reload();.
I've been looking for a way to achieve this behavior and I found this sample project.
The trick in this project is that it changes the form target to an iframe created on the fly.
So far so good, I can get the byte[] on the server-side. But I need to change an image preview after the file is uploaded.
How can I get the iframe to update the main page? Would I have to save it on a file on the disk, make a javascript callback to change the image url? Is there another way to do this? What's recommended?
This control suggested by vorrtex actually causes a full page postback, am I missing something or is it the correct behavior?
I recommend you to use AsyncFileUploader and UpdatePanel. You have to save file on the disk but you can use C# for changing imageUrl.
Add a <script> block to the page that gets loaded in the <iframe> that interacts with the parent page and updates whatever you need to.
You can use the project mentioned below to preview the image before uploading. Working sample is also attached.
http://www.dotnetspider.com/resources/40858-Preview-Image-before-uploading-using.aspx
This uses AjaxControlToolKit's AsyncFileUploadControl and HTTPHandler to upload the image.