【问题标题】:WCF Service not save image from Ionic AppWCF 服务不从 Ionic App 保存图像
【发布时间】:2016-10-14 20:57:36
【问题描述】:

我正在开发一个应用程序并上传图像 Ionic 我的 WCF 服务,问题是当图像来自我的 WCF 代码时没有正确保存。另存为图片无效。

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "upload2/{fileName}")]
string Upload2(string fileName, Stream fileStream);

public string Upload2(string fileName, Stream fileStream)
    {

        try
        {
            FileStream fileToupload = new FileStream(WebConfigurationManager.AppSettings["FilePath"] + fileName, FileMode.Create);

            byte[] bytearray = new byte[10000];
            int bytesRead, totalBytesRead = 0;
            do
            {
                bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
                totalBytesRead += bytesRead;
            } while (bytesRead > 0);

            fileToupload.Write(bytearray, 0, bytearray.Length);
            fileToupload.Close();
            fileToupload.Dispose();
            return "succ";
        }
        catch (Exception ex)
        {
            return ex.Message + " - " + ex.InnerException;
        }
    }

代码控制器 AngularJs,

$scope.subirFoto = function() {
    var options = new FileUploadOptions();

    options.fileKey = "post";

    options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
    options.mimeType = "image/jpeg";
    options.chunkedMode = false;


    var ft = new FileTransfer();

    ft.upload(imageURI, encodeURI("http://192.68.1.182:8085/IServiceTopStore.svc/upload2/"+options.fileName), win, fail, options);
}

【问题讨论】:

    标签: c# angularjs wcf ionic-framework file-transfer


    【解决方案1】:

    有人需要就解决

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "UploadImage/{fileName}")]
    string UploadImage(string fileName);
    
    public string UploadImage(string fileName)
        {
            try
            {
                HttpPostedFile file = HttpContext.Current.Request.Files["post"];
                if (file == null)
                    return null;
                string targetFilePath = WebConfigurationManager.AppSettings["FilePath"] + fileName;
                file.SaveAs(targetFilePath);
                return "succ   " + file.FileName.ToString(); ;
            }
            catch (Exception ex)
            {
                return ex.Message + " - " + ex.InnerException;
            }
        }
    

    离子:

    $scope.subirFoto = function() {
        var options = new FileUploadOptions();
    
        options.fileKey = "post";
    
        options.fileName = imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType = "image/jpeg";
        options.chunkedMode = false;
    
    
        var ft = new FileTransfer();
    
        ft.upload(imageURI, encodeURI("http://192.1.1.1:8085/IServiceTopStore.svc/upload2/"+options.fileName), win, fail, options);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 2020-10-06
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2013-05-16
      相关资源
      最近更新 更多