【问题标题】:video cannot play after upload from retrofit to wcf从改造上传到 wcf 后视频无法播放
【发布时间】:2019-10-08 12:45:55
【问题描述】:

我想通过改造上传视频到 WCF 服务。我也可以上传视频,并且可以将此视频保存在服务器上的特定路径中。 但是,不幸的是,该视频在任何媒体播放器存储在服务器上后都无法播放。我尝试了“WCF”和“Android Java”的这些代码,如下所示:

在 Android 中:

@Multipart
    @POST("MYSERVER/UploadVideo/")
    @Headers({
            "Content-Type: multipart/form-data",
            "Accept: application/json;charset=utf-8"
    })
    Call<Verify> uploadVideo(
            @Part MultipartBody.Part images

    );

 RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);

fileBody =
  MultipartBody.Part.createFormData("videoData", file.getName(), requestFile);

Call<Verify> call = service.uploadVideo(fileBody);

在 WCF 服务中:

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "/UploadVideo/",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        VerifyModel UploadVideo(Stream video);




 string apPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Uploads\\Videos\\" + videoname;



using (FileStream fs = new FileStream(apPath, FileMode.CreateNew, FileAccess.Write))
{
   CopyStream(video, fs);
 }

public static void CopyStream(Stream input, Stream output)
        {
            byte[] buffer = new byte[8 * 1024];
            int len;
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, len);
            }
        }

如前所述,我可以以正确的大小和 mp4 格式保存视频。但视频无法使用任何媒体播放器播放

【问题讨论】:

    标签: c# android wcf upload retrofit


    【解决方案1】:
     I hope this will helpful for you. Retrofit with rxJava
    
    
     @Headers({
            "Accept: application/json"
     })
      @Multipart
      @POST("/api/uploadfiles")
       Observable<UploadFileModel> uploadData(@Header("Authorization") String 
       headerKey, @Part("destFolderPath") RequestBody destFolderPath, @Part 
        MultipartBody.Part file);
    
    
    
    
    /**
     *  
     * @param fileName Name of Uploaded File here
     * @param mineType MineType of uploaded File
     * @param byteArray i convert the uri to byte array by InputStream  you can use 
      file also
     */
    
    
    public void getData(String fileName,String mineType , byte byteArray[]) {
    
        //
        Log.e("File Name", fileName);
        Log.e("MineType", mineType);
    
    
    
          String path = ""; // this is destination folder path where i want to upload the file
        RequestBody description = RequestBody.create(MultipartBody.FORM, path);
    
    
        String file_Name = fileName;
    
        // create RequestBody instance from file
        RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), byteArray);
        // MultipartBody.Part is used to send also the actual file name
    
        MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file_Name, requestBody);
    
    
        AppController appController = AppController.create(mContext);
        ApiInterface apiInterface = appController.getUserService();
    
        Disposable disposable = apiInterface.uploadData(sharedPreferences.getString(TOEKNTYPE, "") + " " + sharedPreferences.getString(ACCESS_TOKEN, ""), description, fileToUpload)
                .subscribeOn(appController.subscribeScheduler())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<UploadFileModel>() {
                    @Override
                    public void accept(UploadFileModel uploadFileModel) {
                        loading.set(GONE);
    
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
    
                        loading.set(GONE);
                        getActivity(mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    
                        Toast.makeText(mContext, "Something wents wrong", Toast.LENGTH_SHORT).show();
    
    
                    }
                });
    
        compositeDisposable.add(disposable);
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      相关资源
      最近更新 更多