Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filename appending random string at the end of filename after upload #806

Open
ankitjasbeersingh opened this issue Dec 11, 2024 · 0 comments

Comments

@ankitjasbeersingh
Copy link

ankitjasbeersingh commented Dec 11, 2024

While uploading the file I'm getting wrong content-type I'm uploading pdf file and when upload completes it append a random string to the filename and can I upload file from file without route directly with creating route

const fileIdToUploadTo = uuidv4(); 
const newBlob = await upload(`users/${user.id}/files/${fileIdToUploadTo}`, file, {
        access: 'public',
        handleUploadUrl: '/api/file/upload',
        onUploadProgress(e) {
            setStatus(StatusText.UPLOADING)
            setProgress(e.percentage);
        }
      });

my api route api/file/upload/route.ts

import { handleUpload, type HandleUploadBody } from '@vercel/blob/client';
import { NextResponse } from 'next/server';
 
export async function POST(request: Request): Promise<NextResponse> {
  const body = (await request.json()) as HandleUploadBody;
 
  try {
    const jsonResponse = await handleUpload({
      body,
      request,
      onBeforeGenerateToken: async (
        pathname
      ) => {
        return {
          tokenPayload: JSON.stringify({
          }),
        };
      },
      onUploadCompleted: async ({ blob, tokenPayload }) => {
        console.log('blob upload completed', blob, tokenPayload);
      },
    });
 
    return NextResponse.json(jsonResponse);
  } catch (error) {
    return NextResponse.json(
      { error: (error as Error).message },
      { status: 400 }, // The webhook will retry 5 times waiting for a 200
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant