The question is not how to transfer the file, but how to transfer the name of the selected file! To pass, as I understand it, you must use FormData. But I only need to get the name of the selected file.
- Get client or server side? - Misha Saidov
- on the server side - user3720167
|
1 answer
I think that might help:
var fullPath = document.getElementById('id').value; if (fullPath) { var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') :fullPath.lastIndexOf('/')); var filename = fullPath.substring(startIndex); if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) { filename = filename.substring(1); } alert(filename); } UPDATE:
$(function () { $('#form').submit(function (e) { e.preventDefault(); var filename = ''; var fullPath = document.getElementById('id').value; // id елемента где файл input if (fullPath) { var startIndex = (fullPath.indexOf('\\') >= 0 ?fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/')); var filename = fullPath.substring(startIndex); if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) { filename = filename.substring(1); } } $.ajax({ type: 'POST', url: '/Home/DoIt', data: { form: $('#form').serialize(), fileName: filename }, dataType: 'html', cache: false }) .done(function (response) { $('#results').html(response) }) .fail(function (response) { console.log(response); $('#results').html(response) }); }); }) - How do I use this in $ (function () {$ ('# form'). Submit (function (e) {$ .ajax ({type: 'POST', url: '/ Home / DoIt', data: $ ( '#form'). serialize (), dataType: 'html', cache: false}). done (function (response) {$ ('# results'). html (response)}). fail (function (response) {console.log (response); $ ('# results'). html (response)}); e.preventDefault ();});}) - user3720167
- Thank! Exactly what is needed! - user3720167
|