I need a way to upload large files (600 mb to 4 gb) in an asp.net mvc website.
Currently I am using swfupload; it works well enough, but it is a huge hit on the webserver because it sends it in one big upload, plus I have to set it in the web.config to allow that huge of a file, which is a huge security risk. In the past when I was doing web forms development I used Neatupload which breaks up the file into chunks and uploads them individually. I am looking for a way to upload large files in mvc that uploads via chunking it up. Any ideas on how I could do this?
Silverlight File Upload
I ended up using Darren Johnstone's ASP.NET File Upload Module to handle the uploading on the server side. Though I modified it slightly so that it could take a guid on the querystring that it would save the temp file to that guid name.
It was nice because it saved the file as it arrived at the server, and stripped the file out of the posted data which it then sends to the action on the controller that was specified.
Example in my view:
<input id="FileGUID" name="FileGUID" type="hidden" value="f632c00b-9b66-4716-8075-79df63b780fb" />
<input type="file" id="FileUpload1" name="fileUpload1" />
<script type="text/javascript">
var UploadUrl = '/Video/AsyncUpload?FileGUID=f632c00b-9b66-4716-8075-79df63b780fb';
$(function() {
$("#FileUpload1").makeAsyncUploader({
upload_url: UploadUrl,
flash_url: '/Content/Flash/swfupload.swf',
button_image_url: '/Content/Images/blank-button.png',
button_text: '<font face="Helvetica, Arial" size="13pt" color="#ffffff">Upload File</font>',
disableDuringUpload: 'input[type="submit"]',
file_size_limit: '8024 MB',
button_text_top_padding: 2
});
});
</script>
Then in the actual save action for this page I look for a file where the asyncupload action would have saved the file based on the FileGUID
A SignalR implementation example can be found here.
This also includes functionality for working with HttpContext.Request.GetBufferlessInputStream(), which allows you to begin working with the post data before it's fully uploaded.
Related
I am using a third party file uploader which is same as asp.net file uploader control.
On a page before post back(clicking a button in gridview) the file name gets empty from file uploader and on code behind can't get file.
There were update panels on page. I have also removed them but no affect.
I've checked the Brettle.Web FAQ http://neatupload.codeplex.com/documentation and found the following answer to a very similar problem:
Q: Why are filenames cleared on postback?
A: Browser security restrictions prevent the server from set the names of files to upload.
I'm developing an ASP.NET web app using VS2010,C#, I want to display a file upload control when my users click on a hyperlink (or label, it doesn't differ), and then the upload operation should be performed, I have no problem working with upload control, but currently I have an invisible upload control which display it using JavaScript in my hyperlink onclick function, the upload control is displayed but I don't know how the get the uploaded file, how should I perform this operation? I want to display upload file dialog when my users click on a label or hyperlink, then they can select their file and the file should be uploaded, and finally I should be able to work with this file in server side (I'm going to get file stream and store it in SQL), what are my options? JQuery? Ajax? JavaScript? or something else?
my JavaScript function:
function OpenFile()
{
document.getElementById("<%=fu.ClientID%>").style.display="";
var result = document.getElementById("<%=fu.ClientID%>").click();
document.getElementById("<%=fu.ClientID%>").style.display="none";
return false;
}
and my markup:
<asp:FileUpload ..... style="display:none;"....>
<ASP:hyperlink onclick="OpenFile();"...../>
After you select a file and do a PostBack, you can access the file as follows
<asp:FileUpload ID="FileUpload1" ..... style="display:none;"....>
Access the underlying posted file using the PostedFile
var fileLen = FileUpload1.PostedFile.ContentLength;
Byte[] Input = new Byte[fileLen];
myStream = FileUpload1.FileContent;
myStream.Read(Input, 0, fileLen);
Or just save it on the server:
FileUpload1.SaveAs(savePath);
I would like to recommend uploadify, it is built with flash. Very simple and easy to use with jQuery and asp.net.
Demo relies php for uploading, though it can be used with any platform including asp.net.
You have to write a handler file to do the uploading, streaming and sql storage part.
Check these answers
by #Zitun https://stackoverflow.com/a/4972872/206766
by #Blankasaurus https://stackoverflow.com/a/2501069/206766
In my ASP.Net website I want to upload a file onClickEvent of LinkButton. I dont have a space to show a FileUpload Control. But if we use fileUpload control it is easy to upload a file using:
String filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/files/") + filename);
How do I open file browser on onClick event of LinkButton and save my file inside the Files folder present on the server?
EDIT:
can we use "OpenFileDialog" From Windows.Forms? If yes, how? i am just asking...
You can use javascript as suggested by Neil Thompson. However, my preferred solution to this issue is the CSS method as described in this post. You can also use the ASP upload control if you wish, and it will be hidden in the same way, allowing you to handle the file in the code behind as you usually would.
Also, as far as I can tell this works in all popular browsers.
I am trying to create an ajaxy file upload.
Here is the asp code:
<input type="file" id="supportingDocs" runat="server"/>
<input type="button" id="uploadBtn" onclick="upload();" value="Upload"/>
Here is the javascript:
function upload()
{
PageMethods.uploadFile($get('supportingDocs').value, onSucceed, onFail);
}
Here is the relevant C# code:
[WebMethod]
[ScriptMethod]
public static string uploadFile(string files) {
HttpRequest request = HttpContext.Current.Request
........
}
What I am trying to get is the HttpFilesCollection from the request which is empty. I know that PageMethods do not follow the normal asp .net lifecycle. However looking at the HttpRequest object while stepping through the code in debugging I see that everything else in the request is there but the "Files" property is empty. I'm probably missing something here and this method of uploading files might not even be possible.
You could always just use the one provided by the AjaxControlToolkit, i.e. ASyncFileUpload
You could consider the jquery plugin: http://www.uploadify.com/
The file upload control works by sending a post request back to the server with the file contents. That is how the file upload is built. with that said, I'm not sure how the inner workings of all these file upload plugins operate, but out of the box even with ASP.NET AJAX services, it may be better to consider some other service.
If you wanted to use a service, you could try the Sys.Net.WebServiceProxy.invoke method, which gives you control over the post, serialize the form data to post to the server. Again, that's in theory, I'm not 100% sure how this all works through AJAX.
HTH.
If you want to upload a file without a full postback, try SWFUpload. It has a very nice file browser option and other features, unlike ASP.NET AJAX AsyncFileUpload which looks very limited.
SWFUpload demo: http://demo.swfupload.org
A popular jquery option is uploadify, see this stackoverflow post.
I never was able to get asynchronous file upload working without flash.
Update
This post claims that AsyncFileUpload doesn't use flash, so that may be an advantage over uploadify and SWFUpload.
Ok so I ended up using both the asp async upload and page methods.
The AsyncFileUpload worked well for actually saving the file on the server but I still needed to run some server-side code to write out the DB and also list the current files uploaded to the server. Unfortunately anything you change client-side will not get rendered since there is no post-back after the upload. So I just set the OnClientUploadComplete to a JS function that uses a page method.
ASP Code:
<asp:AsyncFileUpload ID="asyncFileUpload" runat="server" OnUploadedComplete="uploadFile" OnClientUploadComplete="showUploadedFiles" ThrobberID="uploading"/>
Javascript:
function showUploadedFiles()
{
PageMethods.getUploadedFiles(onSucceed, onFail);
}
function deleteDoc(docId)
{
PageMethods.deleteDocument(docId, onSucceed, onFail);
}
function onSucceed(result, userContext, methodName)
{
$get('uploadedFiles').innerHTML = result;
}
function onFail(error, userContext, methodName)
{
$get('uploadedFiles').innerHTML = "<p style='color: red;'>AJAX Error!</p>";
}
I know how to upload with ASP.net's FileUpload control.
What I want to do is use this jQuery Multiple File Upload Plugin to upload multiple files.
Here is exactly what it does when multiple files are selected for upload:
<input type="file class="multi MultiFile" id="MultiFile1_F3" name="file1[]" style="position: absolute; top: -3000px;">
But I cannot figure out how to manipulate these files from asp.net.
I have tried using Request.Files as the following link instructs:
ASP.Net Upload of multiple files after choosing them from jQuery
That doesn't work. I think that only works for controls marked with runat="server" at compile time.
Does anyone know how to do this? Maybe something in Request.Form...?
Thanks for your help!
Two things to check:
Make sure your form has the enctype="multipart/form-data" attribute set. This is required to enable uploads.
Make sure all file inputs have both id and name attributes set. For some reason, if you don't set both, wierd things happen.
Also, runat="server" shouldn't have anything to do with whether Request.Files works or not -- this is more an issue of the browser actually posting the files.
This jQuery plugin was giving every generated input control the exact same name attribute.
For this reason, the files were not posting.
I built my own javascript solution.
I will post a link to the code in a comment.
Edit
I revisited this and found that what I was trying to do wasn't very difficult at all. I got the the jquery multiple file upload plugin to work fine with my aspx form. I don't know why I was having so much trouble before.
1.) Include the jQuery library on the web form:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" />
2.) Reference the multiple file plugin on the web form (Download it here):
<script src="jquery.MultiFile.pack.js" type="text/javascript">
3.) Add a file input on your web form with class="multi":
<input type="file" class="multi" />
4.) Execute some code or call a method like this on form submission:
void SendMail(string from, string to, string subject, string body, string smtpServer)
{
// create mail message
MailMessage mail = new MailMessage(from, to, subject, body);
// attach posted files
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
mail.Attachments.Add(new Attachment(file.InputStream, file.FileName));
}
//send email
new SmtpClient(smtpServer).Send(mail);
}
This is all that I had to do to attach multiple files to an email sent from an aspx page.
If you want to increase the total size of the files that can be uploaded, add this to your web.config file:
<system.web>
<httpRuntime executionTimeout="240" maxRequestLength="30720"/>
</system.web>
The executionTimeout is measured in seconds and maxRequestLength is measured in kilobytes. In this example, the request will timeout after 4 minutes and will allow a 30mb request.
It's been a bit since I did that kind of thing in .NET, but once you begin cloning form inputs dynamically, I think you have to go out to Request.Form and find the submitted values manually. I wrote up the jQuery code to clone some (non-file) inputs with sequential identifiers here. As long as you have unique identifiers, you can run a loop to see if Request.Form["MultiFile1_F" + counter] exists and go from there.
I highly recommend Uploadify as a mulitple file uploader. It uses jquery and flash to allow the user to upload multiple files at once through ctrl + clicking on all desired files. It then displays a queue of the files uploading and removes the file from the queue on completion. It also allows you to specify which extension to allow the user to upload as well which prevents you from having to do extension validation.
EDIT:
If you dont want to use flash Ajax Upload works really well too. If users on my site company's site dont have the right version of flash that works best with uploadify, I switch to Ajax Upload. They both work very well for me.