未找到从 ASP .Net MVC 返回到 android 的服务器响应代码

Not Found ServerResponse code returned to android from ASP .Net MVC

最近在写一个上传视频到服务器的代码。发布到 php.

时工作正常

PHP 与 android 配合使用的代码:

<?php   
   $file_path = $_SERVER['DOCUMENT_ROOT'].'/uploads/'; //$_SERVER['DOCUMENT_ROOT'] is neccessary and slash before 'uploads'

   $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
   if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
       echo "success".$file_path;
   } else{
       echo "fail";
   }

?>

ASP .Net 代码 returns 未找到服务器对 android

的响应
[HttpPost]
   public HttpResponseMessage UploadFile(HttpPostedFileBase file)
   {
       try
       {
           if (file.ContentLength > 0)
           {
               string _FileName = Path.GetFileName(file.FileName);
               string _path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Videos"), _FileName);
               file.SaveAs(_path);
           }
           return Request.CreateResponse(HttpStatusCode.OK, "success");
       }
       catch
       {
           return Request.CreateResponse(HttpStatusCode.BadRequest, "failed");
       }
   }

Android 上传视频的代码

    private int uploadVideo(String sourceFileUri) {
    String fileName = sourceFileUri;

    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);

    if (!sourceFile.isFile()) {

        dialog.dismiss();

        Log.e("uploadFile", "Source File not exist :" + selectedVideoPath);

        runOnUiThread(new Runnable() {
            public void run() {
                videoURL.setText("Source File not exist :" + selectedVideoPath);
            }
        });

        return 0;

    } else {
        try {
            FileInputStream fileInputStream = new FileInputStream(sourceFile);
            URL url = new URL("http://domain.online/api/User/FileUpload");
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", fileName);

            dos = new DataOutputStream(conn.getOutputStream());

            dos.writeBytes(twoHyphens + boundary + lineEnd);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + fileName + "\"" + lineEnd);

            dos.writeBytes(lineEnd);

            // create a buffer of maximum size
            bytesAvailable = fileInputStream.available();

            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // read file and write it into form...
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {

                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            }

            // send multipart form data necesssary after file data...
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            serverResponseCode = conn.getResponseCode();
            final String serverResponseMessage = conn.getResponseMessage();

            Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);

            if (serverResponseCode == 200) {

                runOnUiThread(new Runnable() {
                    public void run() {
                        String msg = "File Upload Completed.\n\n See uploaded file here : \n\n" + serverResponseMessage;
                        videoURL.setText(msg);
                        Toast.makeText(PostNewTarget.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                    }
                });
            } else {
                runOnUiThread(new Runnable() {
                    public void run() {
                        String msg = "File Upload Completed.\n\n See uploaded file here : \n\n" + serverResponseMessage;
                        videoURL.setText(msg);
                        Toast.makeText(PostNewTarget.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            // close the streams //
            fileInputStream.close();
            dos.flush();
            dos.close();

        } catch (MalformedURLException ex) {

            dialog.dismiss();
            ex.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(PostNewTarget.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                }
            });

            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch (Exception e) {

            dialog.dismiss();
            e.printStackTrace();

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(PostNewTarget.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show();
                }
            });
            Log.e("Upload f - s Exception", "Exception : " + e.getMessage(), e);
        }
        dialog.dismiss();
        return serverResponseCode;
    }
}

有没有更好的方法来接收 .Net 上的视频文件? 这与IIS有关吗? 我在这里错过了什么

问题已解决。实际上我们必须在 ASP .NET 中接收视频文件作为 MultipartFileData 以下是用于执行此操作的 ASP .Net 代码。 CustomMultipartFormDataStreamProvider class 有助于按原样保存视频文件名。

public class UploadController : ApiController
{
[HttpPost]
   public async Task<HttpResponseMessage> PostUpload()
   {
       // Check whether the POST operation is MultiPart?
       if (!Request.Content.IsMimeMultipartContent())
       {
           throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
       }

       // Prepare CustomMultipartFormDataStreamProvider in which our multipart form
       // data will be loaded.
       string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data");
       CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);
       List<string> files = new List<string>();

       try
       {
           // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
           await Request.Content.ReadAsMultipartAsync(provider);

           foreach (MultipartFileData file in provider.FileData)
           {
               files.Add(Path.GetFileName(file.LocalFileName));
           }

           // Send OK Response along with saved file names to the client.
           return Request.CreateResponse(HttpStatusCode.OK, files);
       }
       catch (System.Exception e)
       {
           return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
       }
   }
}
   // We implement MultipartFormDataStreamProvider to override the filename of File which
   // will be stored on server, or else the default name will be of the format like Body-
   // Part_{GUID}. In the following implementation we simply get the FileName from
   // ContentDisposition Header of the Request Body.

public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider {
   public CustomMultipartFormDataStreamProvider(string path) : base(path) { }
   public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers) {
           return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
   }
}