C# 基于ajax文件上传的WebKitFormBoundary

C# 基于ajax文件上传的WebKitFormBoundary,c#,jquery,ajax,C#,Jquery,Ajax,我在通过ajax上传文件时遇到了一些问题。每当我试图上传文件时,它都会附加额外的行,这些行在保存文件时会损坏文件。像这样 ------WebKitFormBoundaryR4wrAzWF9BYdge12 Content-Disposition: form-data; name="files"; filename="testing - Copy.txt" Content-Type: text/plain this is a test ------WebKitFormBoundaryR4wrAz

我在通过ajax上传文件时遇到了一些问题。每当我试图上传文件时,它都会附加额外的行,这些行在保存文件时会损坏文件。像这样

 ------WebKitFormBoundaryR4wrAzWF9BYdge12
Content-Disposition: form-data; name="files"; filename="testing - Copy.txt"
Content-Type: text/plain

this is a test
------WebKitFormBoundaryR4wrAzWF9BYdge12--
这是我的html

var formData = new FormData();
formData.append("files", files[x], files[x].name);
var tempUrl = url + "&filename=" + files[x].name;
console.log(tempUrl, formData);

$.ajax({
    url: tempUrl,
    type: "Post",
    data: formData,
    processData: false,
    contentType: false,//"multipart/form-data",
    success: function () {
        console.log("done!")
    }});
这是C#后端。我没有写这篇文章,而且我对C#也不是很有经验,如果我错过了一个简单的修复,那么很抱歉

public void ProcessRequest(HttpContext context, string tenant, string userId, int parentId)
{
    try
    {
        string filename = context.Request.QueryString["filename"];
        bool complete = string.IsNullOrEmpty(context.Request.QueryString["Complete"]) ? true : bool.Parse(context.Request.QueryString["Complete"]);
        bool getBytes = string.IsNullOrEmpty(context.Request.QueryString["GetBytes"]) ? false : bool.Parse(context.Request.QueryString["GetBytes"]);
        long startByte = string.IsNullOrEmpty(context.Request.QueryString["StartByte"]) ? 0 : long.Parse(context.Request.QueryString["StartByte"]); ;

        if (getBytes)
        {
            context.Response.Write("0");
            context.Response.Flush();
            return;
        }
        else
        {
            int fileSize = (int)context.Request.InputStream.Length;
            StructureManager manager = new StructureManager();

            string fileType = filename.Substring(filename.LastIndexOf("."));
            AcxiomPortal.SharedObjects.PortalService.File file = new AcxiomPortal.SharedObjects.PortalService.File();
            string tempFileName = Path.GetRandomFileName();
            file = manager.CreateFile(tenant, tempFileName, parentId, userId, fileSize);

            try
            {
                SaveFile(context.Request.InputStream, file.UUID);
            }
            catch (Exception ex)
            {
                manager.DeleteFile(file.NodeID);
                throw new Exception(ex.Message, ex.InnerException);
            }

            manager.FinishUploading(tenant, file.NodeID, filename, userId);
            manager.LogActivity("Add", "Document Repository", string.Format("Add {0}-{1} to {2};", file.NodeID, file.Name, parentId), userId, "", tenant);

            if (complete)
            {
                if (FileUploadCompleted != null)
                {
                    FileUploadCompletedEventArgs args = new FileUploadCompletedEventArgs(filename, "");
                    FileUploadCompleted(this, args);
                }
            }
        }
    }
    catch (Exception ex)
    { throw new Exception("DC.SilverlightFileUpload.ProcessRequest(): " + ex.Message, ex.InnerException); }
}
SaveFile
方法如下所示:

private void SaveFile(Stream stream, string guid)
{
    try
    {
        byte[] buffer;

        int blockSize = 1024 * 2048;
        int bytesToRead = (int)stream.Length;
        int bytesRead = 0;

        using(DocumentServiceClient client = new DocumentServiceClient())
        {
            //buffer = new byte[stream.Length];
            //int n = stream.Read(buffer, 0, buffer.Length);

            //client.UploadFile(guid, buffer);

            while (bytesToRead > 0)
            {
                int length = blockSize < bytesToRead ? blockSize : bytesToRead;
                buffer = new byte[length];
                int n = stream.Read(buffer, 0, length);

                if (n == 0)
                    break;

                client.UploadFileByChunk(guid, buffer);

                bytesToRead -= n;
                bytesRead += n;
            }
        }
    }
    catch (Exception ex)
    {
        throw new Exception("DC.SilverlightFileUpload.SaveFile(): " + ex.Message, ex.InnerException);
    }
}
private void保存文件(流、字符串guid)
{
尝试
{
字节[]缓冲区;
int blockSize=1024*2048;
int bytesToRead=(int)stream.Length;
int字节读取=0;
使用(DocumentServiceClient=new DocumentServiceClient())
{
//缓冲区=新字节[stream.Length];
//int n=stream.Read(buffer,0,buffer.Length);
//上载文件(guid、缓冲区);
while(bytesToRead>0)
{
int length=blockSize
我也遇到了类似的情况。你解决了这个问题吗?