无法上传音频/视频

时间:2014-02-09 17:18:41

标签: c# javascript asp.net ajax knockout.js

这是我要选择的html部分,并将文件发布到服务器中的方法。

<div class="control-group">
                    <input type="hidden" name="id" data-bind="value: id" />
                    <div class="controls">
                        <div class="fileupload fileupload-new" data-provides="fileupload">
                            <div class="fileupload-new thumbnail" style="width: 200px; height: 150px;">
                                <embed height="50" width="100" data-bind="attr: { src: mediaUrl() }" />
                            </div>
                            <div class="fileupload-preview fileupload-exists thumbnail" style="max-width: 200px; max-height: 150px; line-height: 20px;">
                            </div>
                            <div>
                                <span class="btn btn-file"><span class="fileupload-new">Choose a Audio/Voice</span> <span class="fileupload-exists">Change</span>
                                    <input type="file" name="photoFile" class="default" />
                                </span><a data-bind="click: addMediaFile" href="#" class="btn fileupload-exists"
                                    data-dismiss="fileupload">Save</a>
                            </div>
                        </div>
                    </div>
                </div>

这是我的ViewModel文件中的addMediaFile javascript函数

self.addMediaFile = function () {
                utils.UIBlock();
                $("#mediaForm").ajaxSubmit({
                    url: "api/Work/EditLibraryItemForMedia",
                    type: "POST",
                    success: function (result) {
                        self.mediaFilePath(result);
                        utils.UIUnBlock();
                        utils.showSuccess("Dosya kaydedilmiştir");
                    },
                    error: function () {
                        utils.UIUnBlock();
                    }
                });
            }

最后这是我服务器中的EditLibraryItemForMedia方法。

private Task<List<string>> UploadMediaFile()
        {
            string folderPath = ConfigurationManager.AppSettings["mediaFilesFolder"].ToString();
            MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(folderPath);
            if (!Request.Content.IsMimeMultipartContent("form-data"))
            {
                return Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(r => { return new List<string>(); });
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
            }

            string resizedImagesFolderPath = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Images/UploadedMedias/"));

            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            return Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(
            readTask =>
            {
                MultipartFormDataStreamProvider provider = readTask.Result;
                return provider.FileData.Select(fileData =>
                {
                    string itemId = provider.FormData["id"];
                    FileInfo fileInfo = new FileInfo(itemId);
                    string mimeType = GetMimeType(fileData.Headers.ContentDisposition.FileName);
                    string extension = "";

                    if (mimeType == "audio/mpeg3" || mimeType == "audio/x-mpeg-3" || mimeType == "video/mpeg" || mimeType == "video/x-mpeg")
                    {
                        extension = ".mp3";
                    }
                    else
                    {
                        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
                    }
                    // Add extensions to files
                    string fileName = itemId + extension;


                    string filePath = Path.Combine(folderPath, fileName);

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    File.Move(fileData.LocalFileName, filePath);

                    WorkService.UpdateMediaFile(itemId, Path.GetFileName(filePath));

                    return fileName;
                }).ToList();
            });
        }

实际上问题是我无法使用此程序上传音频和视频。

0 个答案:

没有答案