Request.Files doesn't work when I deploy to the server but it works when I test locally. What do I need to configure to make this work when deployed?
Some example of your code would be nice, but this post helped me once:
A Back To Basics Case Study: Implementing HTTP File Upload with ASP.NET MVC including Tests and Mocks
This post from Phil Haack can be useful for you:
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" />
</form>
[HttpPost]
public ActionResult Index(HttpPostedFileBase file) {
if (file.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
Note that here you do not examine the Request.Files collection. Also example for the multiple file upload can be found there (main idea that you should use the IEnumerable<HttpPostedFileBase> in similar manner).
Related
It is necessary that the file is downloaded either staying on the current page, or a new one opens. Tell me how to do it. Here is what has already been done.
public FileResult DownloadFile(int id)
{
FilesDB filesDB = _context.FilesDB.Find(id);
filesDB.DownloadCount++;
_context.SaveChanges();
string downloadName= filesDB.FileDisplayName +
Path.GetExtension(filesDB.FilePath);
return File(filesDB.FilePath, filesDB.FileType, downloadName);
}
You can download both way. Just use HTML form.
Here I'm using WebService:
<form id="downloadForm" action="MyService.asmx/DownloadFile" method="post" target="_self" >
<input type="hidden" name="id" id="txtID" />
</form>
JS:
function downloadFile() {
$('#txtID').val(id);
$('#downloadForm').submit();
}
Just call downloadFile() and this will download staying on current page.
If you want to open in new one just change target="_self" to target="_blank"
I have a problem when uploading using multipartfile. Always return response 400. Required request part files is not present. I have tried many ways. but none worked
i have tried to register CommonsMultipartResolver. but still didnt work for me.
#RequestMapping(value = "/viewFormReportUpload.html",method = RequestMethod.POST)
public ModelAndView uploadFormPage(#ModelAttribute("viewForm") ViewFormDownload viewForm,
#RequestParam("file") MultipartFile files, BindingResult result) throws IOException {
//some code
}
<form:input path="files" type="file" id="file" name="files" accept=".xls,.xlsx" class="form-control" />
try this
<form:input path="files" type="file" id="file" name="file" accept=".xls,.xlsx" class="form-control" />
I have a form and in a HTML form inside which I have file upload button.
File is going to be uploaded using AJAX request.
On file upload I am checking extension and file size.
I am uploading file into /home/xyz/upload/username/username_timestamp
But since I am using AJAX request, I cannot(and don't want to) use the CAPTCHA and there is a risk of attacker flooding with files. How can I deal with this issue?
PS: I am using Spring-MVC(not spring security) and Struts2 in my projects, so framework specific solution will be icing on a cake.
Protect your action to be called out of expected by using Strut's token mechanism and your expected logic of file uploads inside your parent action (e.g. max uploads per minutes) ; something like below:
index.jsp
<form action="upload" method="post" enctype="multipart/form-data">
<label for="myFile">Upload your file</label>
<input type="file" name="myFile" />
<s:if test="uploadsPerMinutes < 10">
<s:token name="tknUpload" /> <!-- *** conditional generate token *** -->
</s:if>
<sj:submit value="Submit Form" targets="myAjaxTarget"/>
</form>
<div id="myAjaxTarget">
</div>
struts.xml
<action name="upload" class="com.upload.FileUpload">
<interceptor-ref name="tokenSession/>
<interceptor-ref name="basicStack"/>
<result name="success" type="stream">...</result>
</action>
<action name="uploadParent" class="com.upload.FileUploadParent">
<result name="success">index.jsp</result>
</action>
FileUploadParent.java
public class FileUploadParent extends ActionSupport{
...
public static int uploadsPerMinutes = 0;
private static DateTime lastUploadTime;
public String execute()
{
...
synchronized(uploadsPerMinutes){
if(currentTime - lastUploadTime > 1min) uploadsPerMinutes=0;
else uploadsPerMinutes++;
lastUploadTime = currentTime;
}
return SUCCESS;
}
public String getUploadsPerMinutes()
{
return uploadsPerMinutes;
}
}
With these, client have to get a token from server for each file upload. These are behind the scene and do not disturb your normal users.
I have:
<form method="POST" name="f">
<input type="file" name="FileUpload" id="FileUpload" />
</form>
I'm sent the file using ajax,using the uplaod.file method:
document.getElementById('FileUpload').onchange = function() {
file = this.files[0];
ajax = new XMLHttpRequest;
ajax.file = file;
//etc..
ajax.open('post','Default.aspx', true);
ajax.setRequestHeader('foo','baa');
ajax.send(file);
}
Request.Forms["FileUplaod"] // don't works
How I get the value (the file) sent by ajax in my asp.net application?
I tried sent by http headers(is not pratice good,I know) but there problem with long length.
Thanks in advance.
try this form attribute :
form action="some/action" method="POST" enctype="multipart/form-data"
See the following code. I'm uploading files, Yet i'm getting different results from difference browsers.
Firefox var path = "about.restaurant.jpg"
yet in ie6 path = "D:\dev\xxxxx\xxxxx\xxxxx\Website\images\about.restaurant.jpg"
foreach (string file in Request.Files)
{
var hpf = Request.Files[file] as HttpPostedFileBase;
var path = hpf.FileName;
}
I had this issue in MVC2 . Just upgraded to MVC3. Same issue exist.
<form action="/Product/SaveUploadImage" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" value="Save" class="button" />
</form>
ah ok i see. so there is method that resolves all this
Path.GetFileName(hpf.FileName)