asp.net-mvc


Using Ajax.BeginForm helper in MVC 5 for uploading picture issue [duplicate]


I'm trying to upload a file using Ajax.BeginForm() but it's not working out.
My View Contains:
#using (Ajax.BeginForm("UploadFile", null, new AjaxOptions { HttpMethod="POST", UpdateTargetId = "result" }, new { enctype = "multipart/form-data" }))
{
<label id="lblUploadNewFile" for="fileUploadControl">Upload New File</label>
<input type="file" name="fileToUpload" id="fileUploadControl"/>
<input id="btnFileUpload" type="submit" value="Upload" />
<span id="result" />
}
and the corresponding Controller is:
[HttpPost]
public string UploadFile(FormCollection formData)
{
HttpPostedFileBase file=null;
try
{
file = Request.Files[0];
}
catch { }
if ( file!=null && file.ContentLength > 0)
{
file.SaveAs(string.Concat(
AppDomain.CurrentDomain.BaseDirectory,
Path.GetFileName(file.FileName)));
return "Successfully Uploaded";
}
else
{
return "Upload Failed, please try again.";
}
}
The problem is that it's uploading the file but no longer doing any asynchronous posts when I remove jquery.unobtrusive-ajax.js. Instead, it does a full post-back.
When I add jquery.unobtrusive-ajax.js in my view, it's doing asynchronous but not sending upload file in form data. No file is being sent to the server in Request.Files[].
You cannot upload files using AJAX. This is not supported. If you want to do that you could either use some file upload plugin such as Uploadify or Blueimp File Upload or use the HTML 5 File API if the client browser supports it.
You can do this without additional libraries.
I came across this little hack, which resolves it nicely
window.addEventListener("submit", function (e) {
var form = e.target;
if (form.getAttribute("enctype") === "multipart/form-data") {
if (form.dataset.ajax) {
e.preventDefault();
e.stopImmediatePropagation();
var xhr = new XMLHttpRequest();
xhr.open(form.method, form.action);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
if (form.dataset.ajaxUpdate) {
var updateTarget = document.querySelector(form.dataset.ajaxUpdate);
if (updateTarget) {
updateTarget.innerHTML = xhr.responseText;
}
}
}
};
xhr.send(new FormData(form));
}
}
}, true);
Found http://www.acnenomor.com/1762557p1/c-mvc3-ajaxbeginform-to-upload-file-not-working

Related Links

MVC 5 Razor code-first EF Junction Table with IdentityUser
MVC Route with condition checking
ElmahR Dashboard throws “Unable to compile template” error when Sending Commands
VS2012 add areas to an existing project? start over? with MVC5 or stick with MVC4
Edit view from HttpPost method
Best practive for logging errors (handled and unhandled) in MVC
ServiceStack request giving 500 for large request
Cross domain session asp mvc in IE and old browsers
Adding MVC Controller scaffolding error
Unable to debug plugin in NopCommerce
Displaying HTML and Images in MultiSelectList ASP.NET MVC Razor
#Html.DisplayFor() value not change after the postback
Location of Validation Message
ASP.NET Identity update column type for primary key Id in AspNetUsers table from a string to and int or a guid, yes or no?
Deploying ASP MVC 5 App with IIS 7.5
validation for dropdown in MVC3

Categories

HOME
azure
uwp
validation
vb.net
unit-testing
magento
xaml
outlook
apacheds
afp
styling
addeventlistener
spacemacs
tin-can-api
autofac
windows-10-desktop
light-inject
sqlconnection
vulkan
export-to-excel
smartsheet-c#-sdk-v2
w2ui
graphcool
libpcap
git-bash
software-distribution
profiling
background-subtraction
google-oauth2
scalability
rrd
text-parsing
ocean
ruamel.yaml
angular2-databinding
pyparsing
pvlib
google-crawlers
pdfstamper
p4-lang
powerbuilder-conversion
flags
ipod-touch
program-slicing
powerpc
move
adal.js
reactivex
column-oriented
tryton
email-notifications
wxhaskell
logstash-jdbc
nuitka
pypyodbc
scrutinizer
gcloud-java
occam-pi
ssha
materialdrawer
ospf
esky
base-conversion
cbc-mode
min3d
javaw
bit.ly
formhelper
aps
nitrogen
xacml3
fig
populate
disk-io
asciiencoding
android-lru-cache
llblgenpro
source-highlighting
tempo
shebang
wpf-4.0
clean-urls
timestamp-with-timezone
libavg
harvest
j#
proxy-classes
android-assets
springboard
ssao
plinqo
coolstorage
alivepdf
myisam
cassandra-0.7
custom-protocol
webshop
posterous
palm-pre
web-based
usenet

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App