You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Anton Pussep <an...@pussep.de> on 2007/11/02 12:03:20 UTC
Struts 2, file tag, accept attribute not working?
Hello,
I am using the following tag for uploading text files:
<s:file name="myDoc" accept="text/plain" />
I expected it to recognise wrong content types, but it accepts any files
I upload. What might be the problem here?
I tried this with Struts 2.0.9 and 2.0.11.
Best,
Anton
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts 2, file tag, accept attribute not working?
Posted by Anton Pussep <an...@pussep.de>.
Modifying FileUploadInterceptor or settings its parameters in struts.xml
is not a proper solution since I have two s:file fields in my form and
both require different content types. I am not looking for a work
around, I rather have the question:
Is the accept attribute working at all and if it does, why isn't it
working for me?
Best,
Anton
Martin Gainty wrote:
> Anton-
>
> I had to take a break to find some paying work which didnt happen..
>
> struts.xml includes your web application specific xml I call it
> YourWebApp.xml
> <include file="YourWebApp.xml" />
>
> YourWebApp.xml includes a basicStack which includes fileUpload
> <struts>
> <package name="YourWebApp" extends="struts-default"
> namespace="/YourWebApp">
> <interceptors>
> <interceptor name="fileUpload"
> class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
> <!-- Basic stack -->
> <interceptor-stack name="basicStack">
> <interceptor-ref name="fileUpload"/>
>
> so if you modify the allowedTypes in FileUploadInterceptor.java to include
> ONLY the types you want
>
> package org.apache.struts2.interceptor;
>
> import java.io.File;
> import java.util.Collection;
> import java.util.Collections;
> import java.util.Enumeration;
> import java.util.HashSet;
> import java.util.Iterator;
> import java.util.Locale;
> import java.util.Map;
> import java.util.Set;
> import java.util.StringTokenizer;
>
> import javax.servlet.http.HttpServletRequest;
>
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.apache.struts2.ServletActionContext;
> import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
>
> import com.opensymphony.xwork2.ActionContext;
> import com.opensymphony.xwork2.ActionInvocation;
> import com.opensymphony.xwork2.ActionProxy;
> import com.opensymphony.xwork2.ValidationAware;
> import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
> import com.opensymphony.xwork2.util.LocalizedTextUtil;
>
> /**
> * <!-- START SNIPPET: description -->
> *
> * Interceptor that is based off of {@link MultiPartRequestWrapper}, which
> is automatically applied for any request that
> * includes a file. It adds the following parameters, where [File Name] is
> the name given to the file uploaded by the
> * HTML form:
> *
> * <ul>
> *
> * <li>[File Name] : File - the actual File</li>
> *
> * <li>[File Name]ContentType : String - the content type of the file</li>
> *
> * <li>[File Name]FileName : String - the actual name of the file uploaded
> (not the HTML name)</li>
> *
> * </ul>
> *
> * <p/> You can get access to these files by merely providing setters in
> your action that correspond to any of the three
> * patterns above, such as setDocument(File document),
> setDocumentContentType(String contentType), etc.
> * <br/>See the example code section.
> *
> * <p/> This interceptor will add several field errors, assuming that the
> action implements {@link ValidationAware}.
> * These error messages are based on several i18n values stored in
> struts-messages.properties, a default i18n file
> * processed for all i18n requests. You can override the text of these
> messages by providing text for the following
> * keys:
> *
> * <ul>
> *
> * <li>struts.messages.error.uploading - a general error that occurs when
> the file could not be uploaded</li>
> *
> * <li>struts.messages.error.file.too.large - occurs when the uploaded file
> is too large</li>
> *
> * <li>struts.messages.error.content.type.not.allowed - occurs when the
> uploaded file does not match the expected
> * content types specified</li>
> *
> * </ul>
> *
> * <!-- END SNIPPET: description -->
> *
> * <p/> <u>Interceptor parameters:</u>
> *
> * <!-- START SNIPPET: parameters -->
> *
> * <ul>
> *
> * <li>maximumSize (optional) - the maximum size (in bytes) that the
> interceptor will allow a file reference to be set
> * on the action. Note, this is <b>not</b> related to the various properties
> found in struts.properties.
> * Default to approximately 2MB.</li>
> *
> * <li>allowedTypes (optional) - a comma separated list of content types
> (ie: text/html) that the interceptor will allow
> * a file reference to be set on the action. If none is specified allow all
> types to be uploaded.</li>
> *
> * </ul>
> *
> * <!-- END SNIPPET: parameters -->
> *
> * <p/> <u>Extending the interceptor:</u>
> *
> * <p/>
> *
> * <!-- START SNIPPET: extending -->
> *
> * You can extend this interceptor and override the {@link #acceptFile}
> method to provide more control over which files
> * are supported and which are not.
> *
> * <!-- END SNIPPET: extending -->
> *
> * <p/> <u>Example code:</u>
> *
> * <pre>
> * <!-- START SNIPPET: example -->
> * <action name="doUpload" class="com.examples.UploadAction">
> * <interceptor-ref name="fileUpload"/>
> * <interceptor-ref name="basicStack"/>
> * <result name="success">good_result.ftl</result>
> * </action>
> * </pre>
> *
> * And then you need to set encoding <code>multipart/form-data</code> in the
> form where the user selects the file to upload.
> * <pre>
> * <a:form action="doUpload" method="post"
> enctype="multipart/form-data">
> * <a:file name="upload" label="File"/>
> * <a:submit/>
> * </a:form>
> * </pre>
> *
> * And then in your action code you'll have access to the File object if you
> provide setters according to the
> * naming convention documented in the start.
> *
> * <pre>
> * public com.examples.UploadAction implements Action {
> * private File file;
> * private String contentType;
> * private String filename;
> *
> * public void setUpload(File file) {
> * this.file = file;
> * }
> *
> * public void setUploadContentType(String contentType) {
> * this.contentType = contentType;
> * }
> *
> * public void setUploadFileName(String filename) {
> * this.filename = filename;
> * }
> *
> * ...
> * }
> * </pre>
> * <!-- END SNIPPET: example -->
> *
> */
> public class FileUploadInterceptor extends AbstractInterceptor {
>
> private static final long serialVersionUID = -4764627478894962478L;
>
> protected static final Log log =
> LogFactory.getLog(FileUploadInterceptor.class);
> private static final String DEFAULT_DELIMITER = ",";
> private static final String DEFAULT_MESSAGE = "no.message.found";
>
> protected Long maximumSize;
> protected String allowedTypes="html,xml"; //modify this
>
> ...
> } //end class
>
> Viel Gluck/
> Martin
> ----- Original Message -----
> From: "Anton Pussep" <an...@pussep.de>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Friday, November 02, 2007 12:03 PM
> Subject: Re: Struts 2, file tag, accept attribute not working?
>
>
>> There is no s:head tag, since I am not using Ajax. Thus it should be the
>> default xhtml.
>>
>> Best,
>> Anton
>>
>> Martin Gainty wrote:
>>> Hi Anton
>>>
>>> which theme is specified in your s:head tag?
>>>
>>> Martin-
>>> ----- Original Message -----
>>> From: "Anton Pussep" <an...@pussep.de>
>>>
>>> To: "Struts Users Mailing List" <us...@struts.apache.org>
>>> Sent: Friday, November 02, 2007 6:03 AM
>>> Subject: Struts 2, file tag, accept attribute not working?
>>>
>>>
>>>> Hello,
>>>>
>>>> I am using the following tag for uploading text files:
>>>>
>>>> <s:file name="myDoc" accept="text/plain" />
>>>>
>>>> I expected it to recognise wrong content types, but it accepts any
> files
>>>> I upload. What might be the problem here?
>>>>
>>>> I tried this with Struts 2.0.9 and 2.0.11.
>>>>
>>>> Best,
>>>> Anton
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>>> For additional commands, e-mail: user-help@struts.apache.org
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org
Re: Struts 2, file tag, accept attribute not working?
Posted by Anton Pussep <an...@pussep.de>.
There is no s:head tag, since I am not using Ajax. Thus it should be the
default xhtml.
Best,
Anton
Martin Gainty wrote:
> Hi Anton
>
> which theme is specified in your s:head tag?
>
> Martin-
> ----- Original Message -----
> From: "Anton Pussep" <an...@pussep.de>
>
> To: "Struts Users Mailing List" <us...@struts.apache.org>
> Sent: Friday, November 02, 2007 6:03 AM
> Subject: Struts 2, file tag, accept attribute not working?
>
>
>> Hello,
>>
>> I am using the following tag for uploading text files:
>>
>> <s:file name="myDoc" accept="text/plain" />
>>
>> I expected it to recognise wrong content types, but it accepts any files
>> I upload. What might be the problem here?
>>
>> I tried this with Struts 2.0.9 and 2.0.11.
>>
>> Best,
>> Anton
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org