将图片上传到Unity中的REST API

时间:2018-02-01 05:00:33

标签: c# rest unity3d image-uploading

我想问一下如何将图像上传到REST API,它接受图像作为参数,来自统一。 REST API是单独开发的,并使用邮递员Like This进行测试。由于我们可以使用邮递员成功上传图像,因此问题在于Unity。

这些是我要上传的代码(sourceTex是要上传的图像(Texture2D)):

public void UploadClicked () {
    statusText.text = "UPLOADING";
    DBManager.API.UpdateProfilePicture(sourceTex.GetRawTextureData(),
        (response) =>
        {
            statusText.text = "UPLOAD SUCCESS";
        },
        (error) =>
        {
            statusText.text = "UPLOAD ERROR";
        }
    );

}

以下是处理REST API的类中的代码:

public void UpdateProfilePicture(byte[] texData,
    System.Action<string> onComplete , System.Action<string> onError = null)
{
    string url = config.restURL + config.updateProfilePicture; //this is the REST URL
    WWWForm data = new WWWForm();
    data.AddBinaryData("profilePictureImage",texData,username+"Pic.png","image/png");
    Dictionary<string,string> header = CreateHeaderWithAuthorization ();//create the header here            
    PostRequest(url,data.data,header,onComplete, onError);
}


WWW PostRequest(string url, byte[] data, Dictionary<string,string> postHeader, System.Action<string> onComplete, System.Action<string> onError) {
    WWW www;
    www = new WWW (url, data, postHeader); 

    StartCoroutine (WaitForRequest (www, onComplete, onError,debugIndex));
    return www;
}


IEnumerator WaitForRequest(WWW www, System.Action<string> onComplete, System.Action<string> onError,int debugIndex) {
    while (!www.isDone) {
        yield return null;
    }
    if (www.error == null) {
        if (onComplete != null)
            onComplete (www.text);
    } else {
        if (onError != null)
            onError (www.error + "|" + www.text);
    }
}

但结果是这样的:

"error": "Cannot cast object 'org.apache.catalina.connector.RequestFacade@7352ae54' with class 'org.apache.catalina.connector.RequestFacade' to class 'org.springframework.web.multipart.MultipartHttpServletRequest'","stacktrace":{"suppressed":[],"message":"Cannot cast object 'org.apache.catalina.connector.RequestFacade@7352ae54' with class 'org.apache.catalina.connector.RequestFacade' to class 'org.springframework.web.multipart.MultipartHttpServletRequest'","class":"org.codehaus.groovy.runtime.typehandling.GroovyCastException","stackTraceDepth":119,"cause":null,"localizedMessage":"Cannot cast object 'org.apache.catalina.connector.RequestFacade@7352ae54' with class 'org.apache.catalina.connector.RequestFacade' to class 'org.springframework.web.multipart.MultipartHttpServletRequest'","stackTrace":[{"className":"org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation","fileName":"DefaultTypeTransformation.java","lineNumber":405,"methodName":"continueCastOnSAM","nativeMethod":false},{"className":"org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation","fileName":"DefaultTypeTransformation.java","lineNumber":319,"methodName":"continueCastOnNumber","nativeMethod":false},{"className":"org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation","fileName":"DefaultTypeTransformation.java","lineNumber":232,"methodName":"castToType","nativeMethod":false},{"className":"org.codehaus.groovy.runtime.ScriptBytecodeAdapter","fileName":"ScriptBytecodeAdapter.java","lineNumber":603,"methodName":"castToType","nativeMethod":false},{"className":"com.bidroyal.api.v1.UserController","fileName":"UserController.groovy","lineNumber":706,"methodName":"$tt__uploadProfilePicture","nativeMethod":false},{"className":"com.bidroyal.api.v1.UserController$_uploadProfilePicture_closure14","fileName":"UserController.groovy","lineNumber":-1,"methodName":"doCall","nativeMethod":false},{"className":"sun.reflect.NativeMethodAccessorImpl","fileName":"NativeMethodAccessorImpl.java","lineNumber":-2,"methodName":"invoke0","nativeMethod":true},{"className":"sun.reflect.NativeMethodAccessorImpl","fileName":"NativeMethodAccessorImpl.java","lineNumber":62,"methodName":"invoke","nativeMethod":false},{"className":"sun.reflect.DelegatingMethodAccessorImpl","fileName":"DelegatingMethodAccessorImpl.java","lineNumber":43,"methodName":"invoke","nativeMethod":false},{"className":"java.lang.reflect.Method","fileName":"Method.java","lineNumber":498,"methodName":"invoke","nativeMethod":false},{"className":"org.codehaus.groovy.reflection.CachedMethod","fileName":"CachedMethod.java","lineNumber":93,"methodName":"invoke","nativeMethod":false},{"className":"groovy.lang.MetaMethod","fileName":"MetaMethod.java","lineNumber":325,"methodName":"doMethodInvoke","nativeMethod":false},{"className":"org.codehaus.groovy.runtime.metaclass.ClosureMetaClass","fileName":"ClosureMetaClass.java","lineNumber":294,"methodName":"invokeMethod","nativeMethod":false},{"className":"groovy.lang.MetaClassImpl","fileName":"MetaClassImpl.java","lineNumber":1027,"methodName":"invokeMethod","nativeMethod":false},{"className":"groovy.lang.Closure","fileName":"Closure.java","lineNumber":414,"methodName":"call","nativeMethod":false},{"className":"groovy.lang.Closure","fileName":"Closure.java","lineNumber":430,"methodName":"call","nativeMethod":false},{"className":"grails.transaction.GrailsTransactionTemplate$2","fileName":"GrailsTransactionTemplate.groovy","lineNumber":96,"methodName":"doInTransaction","nativeMethod":false},{"className":"org.springframework.transaction.support.TransactionTemplate","fileName":"TransactionTemplate.java","lineNumber":133,"methodName":"execute","nativeMethod":false},{"className":"grails.transaction.GrailsTransactionTemplate","fileName":"GrailsTransactionTemplate.groovy","lineNumber":93,"methodName":"execute","nativeMethod":false},{"className":"com.bidroyal.api.v1.UserController","fileName":"UserController.groovy","lineNumber":-1,"methodName":"uploadProfilePicture","nativeMethod":false},{"className":"org.grails.core.DefaultGrailsControllerClass$MethodHandleInvoker","fileName":"DefaultGrailsControllerClass.java","lineNumber":223,"methodName":"invoke","nativeMethod":false},{"className":"org.grails.core.DefaultGrailsControllerClass","fileName":"DefaultGrailsControllerClass.java","lineNumber":188,"methodName":"invoke","nativeMethod":false},{"className":"org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter","fileName":"UrlMappingsInfoHandlerAdapter.groovy","lineNumber":90,"methodName":"handle","nativeMethod":false},{"className":"org.springframework.web.servlet.DispatcherServlet","fileName":"DispatcherServlet.java","lineNumber":963,"methodName":"doDispatch","nativeMethod":false},{"className":"org.springframework.web.servlet.DispatcherServlet","fileName":"DispatcherServlet.java","lineNumber":897,"methodName":"doService","nativeMethod":false},{"className":"org.springframework.web.servlet.FrameworkServlet","fileName":"FrameworkServlet.java","lineNumber":970,"methodName":"processRequest","nativeMethod":false},{"className":"org.springframework.web.servlet.FrameworkServlet","fileName":"FrameworkServlet.java","lineNumber":872,"methodName":"doPost","nativeMethod":false},{"className":"javax.servlet.http.HttpServlet","fileName":"HttpServlet.java","lineNumber":648,"methodName":"service","nativeMethod":false},{"className":"org.springframework.web.servlet.FrameworkServlet","fileName":"FrameworkServlet.java","lineNumber":846,"methodName":"service","nativeMethod":false},{"className":"javax.servlet.http.HttpServlet","fileName":"HttpServlet.java","lineNumber":729,"methodName":"service","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":292,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.tomcat.websocket.server.WsFilter","fileName":"WsFilter.java","lineNumber":52,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"javax.servlet.FilterChain$doFilter$0","fileName":null,"lineNumber":-1,"methodName":"call","nativeMethod":false},{"className":"grails.plugin.springsecurity.rest.RestLogoutFilter","fileName":"RestLogoutFilter.groovy","lineNumber":80,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.boot.web.filter.ApplicationContextHeaderFilter","fileName":"ApplicationContextHeaderFilter.java","lineNumber":55,"methodName":"doFilterInternal","nativeMethod":false},{"className":"org.springframework.web.filter.OncePerRequestFilter","fileName":"OncePerRequestFilter.java","lineNumber":107,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.boot.actuate.trace.WebRequestTraceFilter","fileName":"WebRequestTraceFilter.java","lineNumber":105,"methodName":"doFilterInternal","nativeMethod":false},{"className":"org.springframework.web.filter.OncePerRequestFilter","fileName":"OncePerRequestFilter.java","lineNumber":107,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":317,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.access.intercept.FilterSecurityInterceptor","fileName":"FilterSecurityInterceptor.java","lineNumber":127,"methodName":"invoke","nativeMethod":false},{"className":"org.springframework.security.web.access.intercept.FilterSecurityInterceptor","fileName":"FilterSecurityInterceptor.java","lineNumber":91,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":331,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.access.ExceptionTranslationFilter","fileName":"ExceptionTranslationFilter.java","lineNumber":115,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":331,"methodName":"doFilter","nativeMethod":false},{"className":"javax.servlet.FilterChain$doFilter","fileName":null,"lineNumber":-1,"methodName":"call","nativeMethod":false},{"className":"grails.plugin.springsecurity.rest.RestTokenValidationFilter","fileName":"RestTokenValidationFilter.groovy","lineNumber":118,"methodName":"processFilterChain","nativeMethod":false},{"className":"sun.reflect.GeneratedMethodAccessor879","fileName":null,"lineNumber":-1,"methodName":"invoke","nativeMethod":false},{"className":"sun.reflect.DelegatingMethodAccessorImpl","fileName":"DelegatingMethodAccessorImpl.java","lineNumber":43,"methodName":"invoke","nativeMethod":false},{"className":"java.lang.reflect.Method","fileName":"Method.java","lineNumber":498,"methodName":"invoke","nativeMethod":false},{"className":"org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce","fileName":"PogoMetaMethodSite.java","lineNumber":210,"methodName":"invoke","nativeMethod":false},{"className":"org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite","fileName":"PogoMetaMethodSite.java","lineNumber":59,"methodName":"callCurrent","nativeMethod":false},{"className":"org.codehaus.groovy.runtime.callsite.AbstractCallSite","fileName":"AbstractCallSite.java","lineNumber":190,"methodName":"callCurrent","nativeMethod":false},{"className":"grails.plugin.springsecurity.rest.RestTokenValidationFilter","fileName":"RestTokenValidationFilter.groovy","lineNumber":84,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":331,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter","fileName":"SecurityContextHolderAwareRequestFilter.java","lineNumber":169,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":331,"methodName":"doFilter","nativeMethod":false},{"className":"javax.servlet.FilterChain$doFilter","fileName":null,"lineNumber":-1,"methodName":"call","nativeMethod":false},{"className":"grails.plugin.springsecurity.rest.RestAuthenticationFilter","fileName":"RestAuthenticationFilter.groovy","lineNumber":143,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":331,"methodName":"doFilter","nativeMethod":false},{"className":"grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter","fileName":"MutableLogoutFilter.groovy","lineNumber":62,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":331,"methodName":"doFilter","nativeMethod":false},{"className":"grails.plugin.springsecurity.web.SecurityRequestHolderFilter","fileName":"SecurityRequestHolderFilter.groovy","lineNumber":58,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy$VirtualFilterChain","fileName":"FilterChainProxy.java","lineNumber":331,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy","fileName":"FilterChainProxy.java","lineNumber":214,"methodName":"doFilterInternal","nativeMethod":false},{"className":"org.springframework.security.web.FilterChainProxy","fileName":"FilterChainProxy.java","lineNumber":177,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"org.grails.web.servlet.mvc.GrailsWebRequestFilter","fileName":"GrailsWebRequestFilter.java","lineNumber":77,"methodName":"doFilterInternal","nativeMethod":false},{"className":"org.springframework.web.filter.OncePerRequestFilter","fileName":"OncePerRequestFilter.java","lineNumber":107,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"org.grails.web.filters.HiddenHttpMethodFilter","fileName":"HiddenHttpMethodFilter.java","lineNumber":67,"methodName":"doFilterInternal","nativeMethod":false},{"className":"org.springframework.web.filter.OncePerRequestFilter","fileName":"OncePerRequestFilter.java","lineNumber":107,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":207,"methodName":"doFilter","nativeMethod":false},{"className":"org.springframework.web.filter.CharacterEncodingFilter","fileName":"CharacterEncodingFilter.java","lineNumber":197,"methodName":"doFilterInternal","nativeMethod":false},{"className":"org.springframework.web.filter.OncePerRequestFilter","fileName":"OncePerRequestFilter.java","lineNumber":107,"methodName":"doFilter","nativeMethod":false},{"className":"org.apache.catalina.core.ApplicationFilterChain","fileName":"ApplicationFilterChain.java","lineNumber":240,"methodName":"internalDoFilter","nativeMethod":false},{"clas...."

所以我迷失在哪里可以修复我的代码。我尝试了几种变体,比如EncodeToPNG(),或者其他没有MIME类型,它仍然是相同的。如果未启用读/写属性,即使EncodeToPNG()也无法工作。但是,如果图像文件可以来自设备,我该怎么做?

感谢。

0 个答案:

没有答案