在浏览器开发者工具中,我检查了网络中的 xhr 选项卡,请求按预期发送。
调试时:调用了上传方法,但
uploadedFile
是null
项目有通过
p:fileUpload
上传的工作文件,所以我认为过滤器的麻烦已经过去了。
这里有:
豆:
import javax.servlet.http.Part;
import com.d2m.view.bean.LetterWFBean;
@Named
@RequestScoped
public class AudioFileUpload {
private Part uploadedFile ;//+setter/getter
@Inject
private LetterBean letterBean;
public void upload() throws IOException{
byte[] blob = uploadedFile.getInputStream().readAllBytes();
letterBean.setAudioNote(blob);
}
...
}
小脸:
<h:form id="hiddenAudioUploadForm" enctype="multipart/form-data" styleClass="ui-helper-hidden">
<h:inputFile id="audioFile" value="#{audioFileUpload.uploadedFile}" />
<p:remoteCommand name="hiddenAudioUploadCommand" action="#{audioFileUpload.upload}" process="@form" update="@none" />
</h:form>
...
<p:button id="uploadAudio_CL" onclick="uploadAudioFile();return false;" icon="fa fa-check" />
JS:
function addAudioFileToUploadFiles(){
const dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([ base64AudioBlob ], "a.ogg", { type: "audio/webm" }));
document.getElementById("hiddenAudioUploadForm:audioFile").files = dataTransfer.files;
}
async function uploadAudioFile(){
addAudioFileToUploadFiles();
hiddenAudioUploadCommand();
}
谢谢——至少阅读!
故障排除内容:
在bean中添加断点,正是在remoteCommand的操作中。
在浏览器中观察开发人员工具的网络选项卡。
对我来说:
累积方法有帮助:
尝试用最简单的“应该有效”的方法,然后积累!
<filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> <init-param> <param-name>thresholdSize</param-name> <param-value>2097152</param-value> </init-param> <init-param> <param-name>uploadDirectory</param-name> <param-value>${primefaces.file.upload.directory}</param-value> </init-param> </filter> <filter-mapping> <filter-name>PrimeFaces FileUpload Filter</filter-name> <servlet-name>Faces Servlet</servlet-name> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>
通过评论它们,实时返回 h:inputFile 并按相关代码的预期完成所有事情!